根据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
作为输出,我想要一个新的数据帧,其中"N0_YLDF"列分为4个,其中每个ZZ的唯一值都有一个新列.我该怎么办?我可以进行分组,但是不知道如何处理分组的对象.
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]