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.