Python - Pandas - Combining rows of multiple columns into single row in dataframe based on categorical value

大城市里の小女人 提交于 2019-12-11 11:27:19

问题


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:

Model Input

I wish to get the desired output in the form equivalent to:

I wish to get the output similar to:

Desired Output

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?


回答1:


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)]


来源:https://stackoverflow.com/questions/48998786/python-pandas-combining-rows-of-multiple-columns-into-single-row-in-datafram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!