简体   繁体   中英

How to reformat this pandas multiindex dataframe?

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.

I created excel for the shown screenshot of the sheetfile

在此处输入图片说明


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

df:

在此处输入图片说明


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

x:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM