基于groupby拆分pandas数据框 [英] Split pandas dataframe based on groupby

查看:47
本文介绍了基于groupby拆分pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据列 ZZ 拆分以下数据框

I want to split the following dataframe based on column 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

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

As output, I want a new DataFrame with the N0_YLDF column split into 4, one new column for each unique value of ZZ. How do I go about this? I can do groupby, but do not know what to do with the grouped object.

推荐答案

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

这篇关于基于groupby拆分pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆