Home > OS >  add values from another column from next row to update values
add values from another column from next row to update values

Time:11-12

I have a dataframe like the one below:

           a         b  
  0       420        50
  1       380        40
  2       390        45

The problem is that I want to update column 'a' by using the first value (420) and adding the value from column 'b' but the next row and update as it goes through so the final data frame will look like this:

           a         b  
  0       420        50
  1       460        40
  2       505        45

any suggestion?

CodePudding user response:

df.assign(a=df['b'].cumsum()   420 - 50)

output:

    a   b
0   420 50
1   460 40
2   505 45

df.loc[0, 'a'] can replace 420 , df.loc[0, 'b'] can replace 50

  • Related