Search code examples
pythonpandasjupyter-notebook

How to add a column with the growth rate in a budget table in Pandas?


I would like to know how can I add a growth rate year to year in the following data in Pandas.

    Date  Total Managed Expenditure
0   2001                      503.2
1   2002                      529.9
2   2003                      559.8
3   2004                      593.2
4   2005                      629.5
5   2006                      652.1
6   2007                      664.3
7   2008                      688.2
8   2009                      732.0
9   2010                      759.2
10  2011                      769.2
11  2012                      759.8
12  2013                      760.6
13  2014                      753.3
14  2015                      757.6
15  2016                      753.9

Solution

  • Use Series.pct_change():

    df['Total Managed Expenditure'].pct_change()
    Out: 
    0          NaN
    1     0.053060
    2     0.056426
    3     0.059664
    4     0.061194
    5     0.035902
    6     0.018709
    7     0.035978
    8     0.063644
    9     0.037158
    10    0.013172
    11   -0.012220
    12    0.001053
    13   -0.009598
    14    0.005708
    15   -0.004884
    Name: Total Managed Expenditure, dtype: float64
    

    To assign it back:

    df['Growth Rate'] = df['Total Managed Expenditure'].pct_change()