python - How to reshape dataframe if they have same index? -


if have dataframe

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

how can reshape dataframe based on index below i.e

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

let's use set_index, groupby, cumcount, , unstack:

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

output:

   0  1 0   b 1  c  d 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -