У меня есть следующий фрейм данных (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()
Как я могу рассчитать для каждой переменной, сценария и региона их% изменения относительно начального года (ie 2015)?
Например:
2016=(2016-2015)/2015
2017=(2017-2015)/2015
...
2021=(2021-2015)/2015