0

I have two columns, column A and column B. From the 2nd rows to the last I want to subtract previous row of column B with current row of column A and store in Column B only. How can I achieve this? I tried by this link but its still not working. Please help

Chetan P
  • 177
  • 2
  • 3
  • 15

1 Answers1

0

Here is how you can achieve it:

import pandas as pd

df = pd.DataFrame({'A':np.random.randint(1,20,5),
                  'B':np.random.randint(1,20,5)})

enter image description here

df['B'] = df.A - df.B.shift(1)

Output:

enter image description here

quest
  • 3,576
  • 2
  • 16
  • 26