Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new dataframe row based on row above

Tags:

python

pandas

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.

like image 648
Michael Avatar asked Apr 01 '26 15:04

Michael


1 Answers

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)

like image 95
David Kaftan Avatar answered Apr 04 '26 03:04

David Kaftan