如何在Pandas的预算表中添加增长率的列?

时间:2017-04-30 17:20:18

标签: python pandas jupyter-notebook

我想知道如何在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

1 个答案:

答案 0 :(得分:4)

使用系列.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

将其分配回来:

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