How to reshape dataframe if they have same index?

前端 未结 3 1378
暗喜
暗喜 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:20

    You can use pivot with cumcount :

    a = df.groupby(level=0).cumcount()
    df = pd.pivot(index=df.index, columns=a, values=df[0])
    

提交回复
热议问题