0

How to turn: Original dataframe

into this: Wanted dataframe

(sorry I don't know how to paste excel table here.)

Been messing around with pandas stack, pivot_table, melt, but can't seem to make it work. I need some ideas. Thanks.

  • 1
    `print(df.to_dict())` for the above sample dataframe and paste it in the question for users to replicate the dataframe into their systems – anky Mar 19 '20 at 04:49
  • does this helps https://stackoverflow.com/questions/27576795/flatten-dataframe-with-multi-index-columns? – Pygirl Mar 19 '20 at 05:38
  • just upload your excel file to somewhere and give us the link then. – Pygirl Mar 19 '20 at 08:39

1 Answers1

0

I created excel for the shown screenshot of the sheetfile

enter image description here


df = pd.read_excel('PythonExport.xlsx', 
                   header=[0,1,2],index_col=[0,1],
                 )

df:

enter image description here


x = df.T.swaplevel(0,1).T.stack().stack().reset_index()
x.columns = ['repeat','condition','day','type','length','weight']
x = x.fillna(0)

x:

enter image description here

Pygirl
  • 12,969
  • 5
  • 30
  • 43