0

I am trying to generate a new column, with values based on multiplying a row of col_a by the new columns (col_b) previous value

desired output:

col_a.  col_b. 
1        na
2        2*1 (2)
3        3*2.(6)
4        4*6.(24)
5        5*24 (120)

I have tried using the following code and think I am close but could use some help

df['col_b'] = df['col_a'].apply(lambda x[n]: x[n] * x[n-1] for n in range(0, len(df)))

Edited as original problem changed. I have now attempted:

def func(df):
    for n in range(1, len(df)):
        df['col_b'] = df['col_a'][n]*df['col_b'][n-1]
        
    return df

But I am only getting NaNs

CarterB
  • 502
  • 1
  • 3
  • 13

0 Answers0