pandas dataframe: как рассчитать процентное изменение относительно начального года (во время t) - PullRequest
0 голосов
/ 16 июня 2020

У меня есть следующий фрейм данных (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

1 Ответ

1 голос
/ 16 июня 2020

Вы можете попробовать вычесть первый элемент из каждой группы, я суммирую значения за тот же год:

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)

Вывод:

   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
...