Home > Software design >  How to calculate cumulative amount based on daily percent change in dataframe?
How to calculate cumulative amount based on daily percent change in dataframe?

Time:01-04

original dataframe is:

  date              percent_change
2022/12/01             2
2022/12/02             -1
2022/12/02             3

I want to assume initial value as 100. and add a new column which shows cumulative value till date.

expected output:

 date              percent_change       cumulative value
2022/12/01             2                    102
2022/12/02             -1                   100.98
2022/12/02             3                    104.0094

CodePudding user response:

Use Series.cumprod with divide by 100 and adding 1, last multiple by 100:

first = 100
df['cumulative value'] = df['percent_change'].div(first).add(1).cumprod().mul(first)
print (df)
            percent_change  cumulative value
date                                        
2022-12-01               2          102.0000
2022-12-02              -1          100.9800
2022-12-02               3          104.0094

CodePudding user response:

Following code snippet fulfills intended behavior.

import pandas as pd
df_dict = {"percent_change": [2,-1,3]}
df = pd.DataFrame(df_dict)
print(df.head())
cum_sum = 100
cum_sum_list = []
for i in df['percent_change']:
    cum_sum = cum_sum   cum_sum * i / 100
    cum_sum_list.append(cum_sum)
df['cumulative'] = cum_sum_list
print(df.head())

The output of the code :

       percent_change  cumulative
0           2    102.0000
1          -1    100.9800
2           3    104.0094
  • Related