Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a pandas dataframe into many columns after groupby

Tags:

python

pandas

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.

like image 863
f4hy Avatar asked Oct 25 '25 14:10

f4hy


1 Answers

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
like image 146
Viktor Kerkez Avatar answered Oct 28 '25 05:10

Viktor Kerkez