How to reshape dataframe if they have same index?

前端 未结 3 1359
暗喜
暗喜 2020-12-07 03:36

If I have a dataframe like

df= pd.DataFrame([\'a\',\'b\',\'c\',\'d\'],index=[0,0,1,1])
   0
0  a
0  b
1  c
1  d

How can I re

3条回答
  •  借酒劲吻你
    2020-12-07 04:29

    Let's use set_index, groupby, cumcount, and unstack:

    df.set_index(df.groupby(level=0).cumcount(), append=True)[0].unstack()
    

    Output:

       0  1
    0  a  b
    1  c  d
    

提交回复
热议问题