我的数据包含最近日期(1-31-2018)和每月百分比变化的值。我需要在系列中填写之前日期的值。
下面的示例数据Dataframe df:
Date pct_change
7/31/2017 99.944%
8/31/2017 100.462%
9/30/2017 100.552%
10/31/2017 100.158%
11/30/2017 100.103%
12/31/2017 100.308%
1/31/2018 100.000%
print(latest_CPI_value)
105.75
预期产出:
Date pct_change CPI_value
7/31/2017 99.944% 104.0940789
8/31/2017 100.462% 104.5745771
9/30/2017 100.552% 105.1515151
10/31/2017 100.158% 105.3174442
11/30/2017 100.103% 105.4257105
12/31/2017 100.308% 105.75
1/31/2018 100.000% 105.75
任何人都可以帮助我。谢谢!
答案 0 :(得分:2)
我们需要将这些%转换为数字然后我们使用cumprod
df['pct_change'] = pd.to_numeric(df['pct_change'].str[:-1])
s = (df['pct_change']/100).cumprod()
s*105.75/s.iloc[-1]
Out[509]:
0 104.092523
1 104.573430
2 105.150676
3 105.316814
4 105.425290
5 105.750000
6 105.750000
Name: pct_change, dtype: float64