Home > Software design >  Trying to use a lambda function to add a new column with cumulative increase of the values in a Data
Trying to use a lambda function to add a new column with cumulative increase of the values in a Data

Time:01-24

Say I have a simple one column DataFrame:

df = pd.DataFrame([0.01,0.02,0.03,0.04,0.05,0.06,0.07])

I am trying to multiply the first two numbers and then multiply the result of that by the third number and then multiply the result of that by the fourth number, so on down the column.

For example:

df['chainlink'] = df.apply(lambda x: (1 x[0])*(1 x[1]))

This Obviously creates a new column with the value 1.0302 in the first row and then NaNs after. What I am then trying to do is (1.0302)(1 0.03) = 1.0611 then (1.0611)(1 0.04) = 1.1036 etc.

The new column should be a sort of cumulative increase of the values.

CodePudding user response:

Check with cumprod

df['new'] = df[0].add(1).cumprod()
0    1.010000
1    1.030200
2    1.061106
3    1.103550
4    1.158728
5    1.228251
6    1.314229
Name: 0, dtype: float64
  •  Tags:  
  • Related