I have below dataframe and want to transpose the columns aftr 3rd column into rows. Please help on this.
df:
country year perc data1 data2 data3
IN 2015 hjk 75 81 96
US 2015 KTM 100 289 632
Results:
country year perc TransposedColumn Value
IN 2015 hjk data1 75
IN 2015 hjk data2 81
IN 2015 hjk data3 96
US 2015 KTM data1 100
US 2015 KTM data2 289
US 2015 KTM data3 632
use melt
:
df.melt(id_vars=['country','year','perc'])
older versions of Pandas:
pd.melt(df, id_vars=['country','year','perc'])
Output:
country year perc variable value
0 IN 2015 hjk data1 75
1 US 2015 KTM data1 100
2 IN 2015 hjk data2 81
3 US 2015 KTM data2 289
4 IN 2015 hjk data3 96
5 US 2015 KTM data3 632
Option #2
df.set_index(['country','year','perc']).stack().reset_index()
Output:
country year perc level_3 0
0 IN 2015 hjk data1 75
1 IN 2015 hjk data2 81
2 IN 2015 hjk data3 96
3 US 2015 KTM data1 100
4 US 2015 KTM data2 289
5 US 2015 KTM data3 632
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.