I have a big set of data with 100+ columns of data structured like:
country_a country_b year variable1 variable2 ...... varaible100
The goal is to have the 100 variables separated into 100 new dataframes, pivoted, and save them into csvs.
Below is the code for transforming one variable:
import pandas as pd
df = pd.DataFrame({
'country_a': ['aa', 'bb', 'cc'],
'country_b': ['xx', 'yy', 'zz'],
'year': [2018, 2019, 2020],
'var_a': [1, 0, 1],
'var_b': [2, 1, 2],
'var_c': [0, 1.6, 2.4]
})
print(df)
country_a country_b year var_a var_b var_c
0 aa xx 2018 1 2 0.0
1 bb yy 2019 0 1 1.6
2 cc zz 2020 1 2 2.4
Then I'd do a pivot:
table=pd.pivot_table(df, values='var_a', index=['country_a','country_b'],columns=['year']).reset_index()
table.to_csv('var_a.csv')
table will look like this:
country_a country_b 2018 2019 2020
0 aa xx 1.0 NaN NaN
1 bb yy NaN 0.0 NaN
2 cc zz NaN NaN 1.0
I asked the first part of the question here: pandas: melt 100+ variables into 100+ new dataframes Now I have problems incorporating the pivot function for the separated dfs...
Thank you a lot!