How to split a pandas dataframe into many columns after groupby

余生长醉 提交于 2021-02-07 09:00:27

问题


I want to be able to use groupby in pandas to group the data by a column, but then split it so each group is its own column in a dataframe.

e.g.:

   time  data
 0    1   2.0
 1    2   3.0
 2    3   4.0
 3    1   2.1
 4    2   3.1
 5    3   4.1
 etc.

into

       data1  data2  ... dataN
 time  
 1     2.0      2.1  ...
 2     3.0      3.1  ...
 3     4.0      4.1  ...

I am sure the place to start is df.groupby('time') but then I can't seem to figure out the right way to use concat (or other function) to build the split data frame that I want. There is probably some simple function I am overlooking in the API.


回答1:


I agree with @PhillipCloud. I assume that this is probably some intermediate step toward the solution of your problem, but maybe it's easier to just go strait to the thing you really want to solve without the intermediat step.

But if this is what you really want, you can do it using:

>>> df.groupby('time').apply(
        lambda g: pd.Series(g['data'].values)
    ).rename(columns=lambda x: 'data%s' % x)

      data0  data1
time              
1         2    2.1
2         3    3.1
3         4    4.1


来源:https://stackoverflow.com/questions/18927238/how-to-split-a-pandas-dataframe-into-many-columns-after-groupby

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