Home > Mobile >  Transpose specific columns using python pandas
Transpose specific columns using python pandas

Time:10-02

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- enter image description here

Output Data Format- enter image description here

CodePudding user response:

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;

enter image description here

Output of df1;

enter image description here

  • Related