I have a dataframe with one column (Change). I would like to create NewColumn which inputs the number 60 on its first row, and for each row after that is given by 'Change' * the previous value in NewColumn + the previous value in NewColumn. Resulting in the sample dataframe below
Index Change NewColumn
0 0.02 60
1 -0.01 59.4
2 0.05 62.37
3 0.02 63.6174
I can achieve this by running the following loop
df['NewColumn'] = 0.00000
for i in range(len(df)):
if i == 0:
df['NewColumn'][i] = 60
else:
df['NewColumn'][i] = df['NewColumn'][i-1] * df['Change'][i] + df['NewColumn'][i-1]
Which does work okay but over a large dataframe it is pretty slow so I'm looking for any faster way to do this.