简体   繁体   中英

Transpose specific columns using python pandas

I want to transpose below data using python pandas. But I am not getting a proper solution as i want transpose specific columns.

Input Data Format- 在此处输入图像描述

Output Data Format- 在此处输入图像描述

Try using melt

df = pd.DataFrame({"ID":["a","a","a","a"],
                   "Date":["date1","date2","date3","date4"],
                   "H1":[1,1,1,1],
                   "H2":[2,2,2,2],
                   "H3":[3,3,3,3],
                   "H4":[4,4,4,4]})
df1 = pd.melt(df,id_vars =['ID','Date'], value_vars =['H1','H2','H3','H4'])

Output of df;

在此处输入图像描述

Output of df1;

在此处输入图像描述

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