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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.