简体   繁体   中英

How to change columns and rows in my csv file?

I have a csv file with this kind of data:

在此处输入图片说明

But I would like to have the columns Americas, Asia, Europe, Africa etc. with all the corresponding "vdem_corr" values under it. How can I change this in python?

df = pd.read_csv(...) 
df.pivot(columns='region', values='vdem_corr')

My solution is:

result = df.groupby('region').apply(lambda grp: pd.Series(
    grp.vdem_corr.values)).unstack(level=1).T

The advantage over the answer by Prahken is that my result has much less NaN values.

Try on your own and compare.

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