Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compute balance column python dataframe with initial static value

i am trying to get a balance column in a python dataframe with an initial static value.

The logic:

start balance = 1000

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

current balance = previous current balance*(1+df[‘return’])

My attempt:

df.at[1,'current balance'] = 1000
df['current balance'] = df['current balance'].shift(1)*(1+df['return])

I can’t get this output

Output dataframe:

return  current balance
0.01    1010.00
0.03    1040.30
0.045   1087.11

>Solution :

Standard compound return:

initial_balance = 1000
df['current balance'] = (1 + df['return']).cumprod() * initial_balance

>>> df
   return  current balance
0   0.010        1010.0000
1   0.030        1040.3000
2   0.045        1087.1135
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading