Python-Pandas-根据分类值将多列的行合并到数据框中的单行 [英] Python - Pandas - Combining rows of multiple columns into single row in dataframe based on categorical value
问题描述
我正在研究Python 3.4中涉及熊猫的问题.我只停留在一个小节中,这涉及重新组织我的数据帧.我将更具体.
I'm working on a problem involving Pandas in Python 3.4. I'm stuck at one small subsection which involves re-organizing my data frames. I shall be more specific.
我有一个名为模型"的表格,格式为:
I have a table called "model" in the format of:
我希望以与以下形式等效的形式获得所需的输出:
I wish to get the desired output in the form equivalent to:
我希望得到类似以下的输出:
I wish to get the output similar to:
我研究了使用python熊猫将具有多行的python数据帧转换为一行?和
I have looked into Convert a python dataframe with multiple rows into one row using python pandas? and How to combine multiple rows into a single row with pandas. I am getting confused on whether I should use groupby, or pivot table. I tried using both but I either get a KeyError or not the right format I wanted. Is there any specific library that can help achieve the above task?
推荐答案
您可以使用groupby并应用:
You can use groupby and apply:
num_V = 5
max_row = df.groupby('ID').ID.count().max()
df2= (
df.groupby('ID')
.apply(lambda x: x.values[:,1:].reshape(1,-1)[0])
.apply(pd.Series)
.fillna(0)
)
df2.columns = ['V{}_{}_{}'.format(i+1,j,i) for j in range(max_row) for i in range(num_V)]
这篇关于Python-Pandas-根据分类值将多列的行合并到数据框中的单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!