首页 文章

基于groupby拆分pandas数据帧

提问于
浏览
26

我想基于ZZ列拆分以下数据帧

df = 
        N0_YLDF  ZZ        MAT
    0  6.286333   2  11.669069
    1  6.317000   6  11.669069
    2  6.324889   6  11.516454
    3  6.320667   5  11.516454
    4  6.325556   5  11.516454
    5  6.359000   6  11.516454
    6  6.359000   6  11.516454
    7  6.361111   7  11.516454
    8  6.360778   7  11.516454
    9  6.361111   6  11.516454

作为输出,我想要一个新的数据帧,其中'N0_YLDF'列分为4个,每个ZZ的唯一值一个新列 . 我该怎么做?我可以做groupby,但不知道如何处理分组对象 .

3 回答

  • 58
    gb = df.groupby('ZZ')    
    [gb.get_group(x) for x in gb.groups]
    
  • 2

    还有另一种选择,因为groupby返回一个生成器,我们可以简单地使用list-comprehension来检索第二个值(帧) .

    df = [x for _, x in df.groupby('ZZ')]
    
  • 3

    在R中有一个名为split的数据帧方法 . 这适用于所有R用户:

    def split(df, group):
         gb = df.groupby(group)
         return [gb.get_group(x) for x in gb.groups]
    

相关问题