Calculating Cumulative Compound Interest

Viewed 156

I have the code below. It works correctly if the deposit is always > 0. However, if it's 0 for some months, then the pandas algorithm breaks. It doesn't return the correct value.

import pandas as pd


deposit = [100] * 4
rate = [0.1] * 4

df = pd.DataFrame({ 'deposit':deposit, 'rate':rate})
df['interest'] = df.deposit * df.rate
df['total'] = df.deposit.cumsum() + df.interest.cumsum()
df.loc[2:, ['deposit']] = 0
df['total'] = (df['deposit'] * df['rate'].shift().add(1).cumprod().fillna(1)).cumsum()

Here's the result


    deposit rate    interest    total
0   100     0.1     10.0        100.0
1   100     0.1     10.0        210.0
2   0       0.1     10.0        210.0
3   0       0.1     10.0        210.0

For months 2 and 3, the amount should increase by the interest accumulated from the previous months on months 1.

1 Answers
import pandas as pd

init_deposit = 100
init_rate = 0.1
deposit = [init_deposit] * 8
rate = [init_rate] * 8

df = pd.DataFrame({ 'deposit':deposit, 'rate':rate})
df.loc[2:4, ['deposit']] = 0
df['interest'] =0
df['total'] =0
df['total'].iloc[0] = init_deposit
for i in range(1, len(df)):
    df['interest'].iloc[i] = df['total'].iloc[i-1] * df['rate'].iloc[i]
    df['total'].iloc[i] = df['interest'].iloc[i] + df['total'].iloc[i-1] + df['deposit'].iloc[i]
Related