简体   繁体   English

如何在groupby之后将pandas数据帧拆分为多个列

[英]How to split a pandas dataframe into many columns after groupby

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. 我希望能够在pandas中使用groupby按列对数据进行分组,然后将其拆分,以便每个组在数据帧中都是自己的列。

eg: 例如:

   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. 我确定开始的地方是df.groupby('time')但是我似乎无法找出使用concat(或其他函数)构建我想要的拆分数据框架的正确方法。 There is probably some simple function I am overlooking in the API. 我可能会在API中忽略一些简单的功能。

I agree with @PhillipCloud. 我同意@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM