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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With