[英]Pandas: How to revert pct_change to the original value, with the initial value?
It is usual to use .pct_change() to have the daily change of time-series data.通常使用 .pct_change() 来获取时间序列数据的每日变化。
Now I want to have the original value by using the pct_change result.现在我想通过使用 pct_change 结果获得原始值。
I have a data frame like this:我有一个这样的数据框:
df = pd.DataFrame({
'value': [44, 45, 33, 56, 60]
})
df['pct_change'] = df['value'].pct_change() # get changes
initial_value=df['value'].values[0] # store the initial value
How can I use df['pct_change']
and initial_value
to get the df['value']
?如何使用df['pct_change']
和initial_value
来获取df['value']
?
You can do cumprod
你可以做cumprod
df['pct_change'].add(1,fill_value=0).cumprod()*44
Out[200]:
0 44.0
1 45.0
2 33.0
3 56.0
4 60.0
Name: pct_change, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.