The diff() method in pandas is used to find the difference between two rows in a pandas dataframe.
periods – The number of previous rows for calculating the difference
axis – find difference over rows (0) or columns (1)
Let’s read a dataset to work with.
import pandas as pd url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv' df = pd.read_csv(url) df.head()
1 . Find difference between subsequent rows –
To find the difference between the current rows and previous row, we can use the diff() method.
2 . Find the difference at different intervals –
By default the diff() method calculate the difference between current row and the previous row. But you can change that using the periods parameter.
Let’s say we want to find the difference between the current row and the 3rd row before it.