0

I have DataFrame:

               dt_object      Coord
1    2020-01-01 00:00:00  279.88210
2    2020-01-01 01:00:00  279.92457
3    2020-01-01 02:00:00  279.96705
4    2020-01-01 03:00:00  280.00953
5    2020-01-01 04:00:00  280.05200

I need to add column Diff with difference of current and previous row. So it will be:

               dt_object      Coord    Diff
1    2020-01-01 00:00:00  279.88210    NaN
2    2020-01-01 01:00:00  279.92457    0.04247
3    2020-01-01 02:00:00  279.96705    0.04248
4    2020-01-01 03:00:00  280.00953    0.04248
5    2020-01-01 04:00:00  280.05200    0.04247

How to do it with pandas? It is easy to do it with iterating. May be there are better ways?

Igor K.
  • 813
  • 6
  • 17

1 Answers1

1

Try this

df['Diff'] = df['Coord'] - df['Coord'].shift(1)
Anurag Reddy
  • 1,159
  • 11
  • 19