如何在groupby后将pandas数据帧拆分为多列 [英] How to split a pandas dataframe into many columns after groupby
问题描述
我希望能够在 Pandas 中使用 groupby 按列对数据进行分组,然后将其拆分,以便每个组在数据框中都是自己的列.
例如:
时间数据0 1 2.01 2 3.02 3 4.03 1 2.14 2 3.15 3 4.1等等.
进入
data1 data2 ... dataN时间1 2.0 2.1 ...2 3.0 3.1 ...3 4.0 4.1 ...
我确定开始的地方是 df.groupby('time') 但是我似乎无法找出使用 concat (或其他函数)来构建我想要的拆分数据框的正确方法.我在 API 中可能忽略了一些简单的功能.
我同意 @PhillipCloud.我认为这可能是解决您的问题的一些中间步骤,但如果没有中间步骤,直接直接解决您真正想要解决的问题可能会更容易.
但如果这是你真正想要的,你可以使用:
<预><代码>>>>df.groupby('time').apply(lambda g: pd.Series(g['data'].values)).rename(columns=lambda x: 'data%s' % x)数据0 数据1时间1 2 2.12 3 3.13 4 4.1I 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
这篇关于如何在groupby后将pandas数据帧拆分为多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!