简体   繁体   中英

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.

Now I want to have the original value by using the pct_change result.

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'] ?

You can do 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM