pandas 如何在python中分组转置数据?

mfpqipee  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(172)

我有一个像这样的大 Dataframe ,

instance_id                         wake_speed_factor
wt_E70491_0.857                                 1,00001
wt_E70492_0.857                                 0,817721
wt_E70490_0.857                                 1,00054
wt_E70486_0.857                                 1
wt_E70493_0.857                                 0,926203
wt_E70484_0.857                                 0,865908
wt_E70487_0.857                                 1
wt_E70489_0.857                                 0,930648
wt_E70483_0.857                                 0,957561
wt_E70485_0.857                                 0,968135
wt_E70488_0.857                                 0,996474
wt_E70491_0.857                                 1,00001
wt_E70492_0.857                                 0,803226
wt_E70490_0.857                                 1,00029
wt_E70486_0.857                                 1
wt_E70493_0.857                                 0,852778
wt_E70484_0.857                                 0,828148
wt_E70487_0.857                                 1,00002
wt_E70489_0.857                                 0,881657
wt_E70483_0.857                                 0,896756
wt_E70485_0.857                                 0,921366
wt_E70488_0.857                                 0,999333
wt_E70491_0.857                                 1,00001
wt_E70492_0.857                                 0,878923
wt_E70490_0.857                                 0,999948
wt_E70486_0.857                                 1
wt_E70493_0.857                                 0,76646
wt_E70484_0.857                                 0,837149
wt_E70487_0.857                                 1,00003
wt_E70489_0.857                                 0,821626
wt_E70483_0.857                                 0,76071
wt_E70485_0.857                                 0,818493
wt_E70488_0.857                                 0,991048

这里的示例ID每11个单元格重复一次。所以,我想把它们分组,并安排如下所示的 Dataframe 。
我不知道如何将这个数据框转换成下面给出的数据框,

wt_E70491_0.857     wt_E70492_0.857     wt_E70490_0.857     wt_E70486_0.857     wt_E70493_0.857     wt_E70484_0.857     wt_E70487_0.857     wt_E70489_0.857     wt_E70483_0.857     wt_E70485_0.857     wt_E70488_0.857  
1,00001              0,817721             1,00054              1                  0,926203           0,865908             1                    0,930648          0,957561            0,968135             0,996474
1,00001              0,803226             1,00029              1                  0,852778           0,828148             1,00002              0,881657          0,896756            0,921366             0,999333
1,00001              0,878923             0,999948             1                  0,76646            0,837149             1,00003              0,821626          0,76071             0,818493             0,991048

我该怎么做?

2wnc66cl

2wnc66cl1#

有关更多信息,请参见How can I pivot a dataframe上问题10的答案:

out = (
    df
    .assign(index=df.groupby("instance_id").cumcount())
    .pivot(columns="instance_id", index="index", values="wake_speed_factor")
)

输出:

instance_id wt_E70483_0.857 wt_E70484_0.857 wt_E70485_0.857 wt_E70486_0.857  ... wt_E70490_0.857 wt_E70491_0.857 wt_E70492_0.857 wt_E70493_0.857
index                                                                        ...
0                  0,957561        0,865908        0,968135               1  ...         1,00054         1,00001        0,817721        0,926203
1                  0,896756        0,828148        0,921366               1  ...         1,00029         1,00001        0,803226        0,852778
2                   0,76071        0,837149        0,818493               1  ...        0,999948         1,00001        0,878923         0,76646

[3 rows x 11 columns]
57hvy0tb

57hvy0tb2#

您可以使用pivot_table,然后进行分解:

pd.pivot_table(
    df, values="wake_speed_factor", columns="instance_id", aggfunc=list
).reset_index(drop=True).explode(df["instance_id"].unique().tolist(), ignore_index=True)

instance_id wt_E70483_0.857 wt_E70484_0.857 wt_E70485_0.857 wt_E70486_0.857 wt_E70487_0.857 wt_E70488_0.857 wt_E70489_0.857 wt_E70490_0.857 wt_E70491_0.857 wt_E70492_0.857 wt_E70493_0.857
0   0,957561    0,865908    0,968135    1   1   0,996474    0,930648    1,00054 1,00001 0,817721    0,926203
1   0,896756    0,828148    0,921366    1   1,00002 0,999333    0,881657    1,00029 1,00001 0,803226    0,852778
2   0,76071 0,837149    0,818493    1   1,00003 0,991048    0,821626    0,999948    1,00001 0,878923    0,76646

相关问题