1

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.

mingsterism
  • 146
  • 3
  • 13

1 Answers1

0
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]
khouzam
  • 263
  • 3
  • 14
  • can we do it without a for loop because i realize the performance of for loops is really bad. – mingsterism Jun 18 '21 at 08:23
  • Unfortunately, your computation is a recursive operation (`df['total'].iloc[i] = df['interest'].iloc[i] + df['total'].iloc[i-1] + df['deposit'].iloc[i]`) . And vectorizing recursive operations is not easy. You can nevertheless try to use numba to speed up the loop. See https://stackoverflow.com/a/38008937/5872695 – greg hor Nov 17 '21 at 09:08