简体   繁体   中英

How to use melt function in pandas for large table?

I currently have data which looks like this:

    Afghanistan_co2 Afghanistan_income  Year    Afghanistan_population  Albania_co2
1   NaN 603 1801    3280000 NaN
2   NaN 603 1802    3280000 NaN
3   NaN 603 1803    3280000 NaN
4   NaN 603 1804    3280000 NaN

and I would like to use melt to turn it into this:

格式化数据

But with the labels instead as 'Year', 'Country', 'population Value',' co2 Value', 'income value'

It is a large dataset with many rows and columns, so I don't know what to do, I only have this so far:

pd.melt(merged_countries_final, id_vars=['Year']) 

I've done this since there does exist a column in the dataset titled 'Year'.

What should I do?

Just doing with str.split with your columns

df.set_index('Year',inplace=True)
df.columns=pd.MultiIndex.from_tuples(df.columns.str.split('_').map(tuple))
df=df.stack(level=0).reset_index().rename(columns={'level_1':'Country'})
df
   Year      Country  co2  income  population
0  1801  Afghanistan  NaN   603.0   3280000.0
1  1802  Afghanistan  NaN   603.0   3280000.0
2  1803  Afghanistan  NaN   603.0   3280000.0
3  1804  Afghanistan  NaN   603.0   3280000.0

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