3

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.

Michael
  • 343
  • 2
  • 7
  • do you mean something like this? https://stackoverflow.com/questions/32257276/pandas-equivalent-to-numpy-roll – n4321d Jun 07 '21 at 22:30
  • Does this answer your question? [Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?](https://stackoverflow.com/questions/34855859/is-there-a-way-in-pandas-to-use-previous-row-value-in-dataframe-apply-when-previ) – ArkF Jun 07 '21 at 22:49

1 Answers1

5

I would use Series.cumprod on a modified change column, then just multiply that to the start value of 60:

df = pd.DataFrame(dict(Change=[0.00, -0.01, 0.05, 0.02]))
multiplier = (df.Change + 1.0).cumprod()
df['New Column'] = multiplier * 60 

df                                                                                                                     
     Change   New Column                                                                                                   
0    0.00     60.0000                                                                                                   
1   -0.01     59.4000                                                                                                   
2    0.05     62.3700                                                                                                   
3    0.02     63.6174  

(I changed the first Change value to zero, because its not clear what the first row of Change means)

David Kaftan
  • 1,974
  • 1
  • 12
  • 18