# How to calculate differences between consecutive rows in pandas data frame?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to calculate differences between consecutive rows in pandas data frame in Python. So Here I am Explain to you all the possible Methods here.

## How to calculate differences between consecutive rows in pandas data frame?

1. How to calculate differences between consecutive rows in pandas data frame?

You can using the `.rolling_apply(…)` method:
`diffs_a = pd.rolling_apply(df['count_a'], 2, lambda x: x[0] - x[1])`

2. calculate differences between consecutive rows in pandas data frame

You can using the `.rolling_apply(…)` method:
`diffs_a = pd.rolling_apply(df['count_a'], 2, lambda x: x[0] - x[1])`

## Method 1

`diff` should give the desired result:

```>>> df.diff()
count_a  count_b
2015-01-01      NaN      NaN
2015-01-02    38465      NaN
2015-01-03    36714      NaN
2015-01-04    35137      NaN
2015-01-05    35864      NaN
....
2015-02-07   142390    25552
2015-02-08   126768    22835
2015-02-09   122324    21485```

## Method 2

You can using the `.rolling_apply(…)` method:

```diffs_a = pd.rolling_apply(df['count_a'], 2, lambda x: x[0] - x[1])
```

Alternatively, if it’s easier, you can operate on the arrays directly:

```count_a_vals = df['count_a'].values
diffs_a = count_a_vals[:-1] - count_a_vals[1:]```

## Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.