Reputation: 15
I have the following dataframe (df):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
import matplotlib as mpl
sns.set()
df = pd.DataFrame({
# some ways to create random data
'scenario':np.random.choice( ['BAU','ETS','ESD'], 27),
'region':np.random.choice( ['Italy','France'], 27),
'variable':np.random.choice( ['GDP','GHG'], 27),
# some ways to create systematic groups for indexing or groupby
# this is similar to r's expand.grid(), see note 2 below
'2015':np.random.randn(27),
'2016':np.random.randn(27),
'2017':np.random.randn(27),
'2018':np.random.randn(27),
'2019':np.random.randn(27),
'2020':np.random.randn(27),
'2021':np.random.randn(27)
})
df2=pd.melt(df,id_vars=['scenario','region','variable'],var_name='year')
all_names_index = df2.set_index(['scenario','region','variable','year']).sort_index()
How can I calculate for each variable, scenario and region its % change with respect to initial year (ie 2015)?
As an example:
2016=(2016-2015)/2015
2017=(2017-2015)/2015
...
2021=(2021-2015)/2015
Upvotes: 0
Views: 187
Reputation: 7594
You can try this to subtract the first element from every group, I'm summing the values for the same year:
all_names_index.reset_index(inplace=True)
all_names_index = all_names_index.groupby(by=['scenario', 'region', 'variable', 'year']).sum().reset_index()
all_names_index['pct_change'] = all_names_index.groupby(by=['scenario', 'region', 'variable'])['value'].apply(lambda x: x.div(x.iloc[0]).subtract(1).mul(100))
print(all_names_index)
Output:
scenario region variable year value pct_change
0 BAU France GDP 2015 1.786506 0.000000
1 BAU France GDP 2016 0.020103 -98.874740
2 BAU France GDP 2017 3.190068 78.564690
3 BAU France GDP 2018 -3.581261 -300.461753
4 BAU France GDP 2019 0.500374 -71.991488
.. ... ... ... ... ... ...
72 ETS Italy GDP 2017 -0.557029 -153.990905
73 ETS Italy GDP 2018 -0.172391 -116.709261
74 ETS Italy GDP 2019 -0.238212 -123.089063
75 ETS Italy GDP 2020 -1.098866 -206.509438
76 ETS Italy GDP 2021 -0.405364 -139.290556
Upvotes: 1