I have a loop that is taking way too much time and I wonder if there is a better way? Or if I am making rookie mistakes?
The reason I am doing a loop is that the first value is different and the need for previous values.
# create var and set to 0
df [ 'amt_model' ] = 0
# create the cashflow variable
df [ 'cf' ] = df [ 'cash_in' ] - df [ 'cash_out' ] + df [ 'transfer' ]
Now I loop through the range of months to create the 'amt_model' values.
for i in range ( len ( df ) ):
# adjust for the first month
if i == 0:
df [ 'amt_model' ].iloc [ i ] = df [ 'contrib' ].iloc [ i ]
else:
amt1 = df [ 'amt_model' ].iloc [ i - 1 ] * (1 + df [ 'pct_model' ].iloc [ i ])
amt2 = df [ 'cf' ] [ i ] * (1 + df [ 'pct_model' ].iloc [ i ] / 2)
df [ 'amt_model' ].iloc [ i ] = amt1 + amt2
This is taking up way too much time to loop through only 20 or 50 values.
index_values- start 19:28
index_values - end 19:42
Thanks!