Pandas DataFrame diff() method with examples

Spread the love

The diff() method in pandas is used to find the difference between two rows in a pandas dataframe.

Syntax –

dataframe.diff(periods=1, axis=0)

periods – The number of previous rows for calculating the difference

axis – find difference over rows (0) or columns (1)

Examples –

Let’s read a dataset to work with.

import pandas as pd

url = ''
df = pd.read_csv(url)

1 . Find difference between subsequent rows –

To find the difference between the current rows and previous row, we can use the diff() method.

df['Net Sales'].diff()

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.

df['Net Sales'].diff(periods=3)

Rating: 1 out of 5.

Leave a Reply