Pandas DataFrame diff() Method

In this tutorial, we will learn the Python pandas DataFrame.diff() method. It provides the first discrete difference of elements. It calculates the difference of a Dataframe element compared with another element in the Dataframe (default is an element in the previous row).

The below shows the syntax of the DataFrame.diff() method.

Syntax

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

Parameters

periods: int, default 1. Periods to shift for calculating difference accepts negative values.

axis:{0 or ‘index’, 1 or ‘columns’}, default 0. Take difference over rows (0) or columns (1).

Example 1: Calculating difference Using the DataFrame.diff() Method

The below example shows the difference with the previous row of the dataframe.

import pandas as pd
df = pd.DataFrame({'a': [1, 3, 8],'b': [3, 5, 8],'c': [16, 25, 36]})
print("------DataFrame-------")
print(df)
print("------Difference with previous row------ ")
print(df.diff())
Once we run the program we will get the following output.


------DataFrame-------
a b c
0 1 3 16
1 3 5 25
2 8 8 36
------Difference with previous row------
a b c
0 NaN NaN NaN
1 2.0 2.0 9.0
2 5.0 3.0 11.0

Example 2: Calculating difference Using the DataFrame.diff() Method

The below example shows the difference between the previous column of the dataframe.

import pandas as pd
df = pd.DataFrame({'a': [1, 3, 8],'b': [3, 5, 8],'c': [16, 25, 36]})
print("------DataFrame-------")
print(df)
print("------Difference with previous columns------ ")
print(df.diff(axis=1))

Once we run the program we will get the following output.


------DataFrame-------
a b c
0 1 3 16
1 3 5 25
2 8 8 36
------Difference with previous columns------
a b c
0 NaN 2.0 13.0
1 NaN 2.0 20.0
2 NaN 0.0 28.0

Example 3: Calculating difference Using the DataFrame.diff() Method

The below example shows the difference with the second previous row of the dataframe.

import pandas as pd
df = pd.DataFrame({'a': [1, 3, 8, 5],'b': [3, 5, 8, 6],'c': [16, 25, 36, 20]})
print("------DataFrame-------")
print(df)
print("------Difference with 2nd previous rows ------ ")
print(df.diff(periods=2))

Once we run the program we will get the following output.


------DataFrame-------
a b c
0 1 3 16
1 3 5 25
2 8 8 36
3 5 6 20
------Difference with 2nd previous rows ------
a b c
0 NaN NaN NaN
1 NaN NaN NaN
2 7.0 5.0 20.0
3 2.0 1.0 -5.0

Example 4: Calculating difference Using the DataFrame.diff() Method

The below example shows the difference with the third previous row of the dataframe.

import pandas as pd
df = pd.DataFrame({'a': [1, 3, 8, 5, 6],'b': [3, 5, 8, 6, 7],'c': [16, 25, 36, 20, 25]})
print("------DataFrame-------")
print(df)
print("------Difference with 3rd previous rows ------ ")
print(df.diff(periods=3))

Once we run the program we will get the following output.


------DataFrame-------
a b c
0 1 3 16
1 3 5 25
2 8 8 36
3 5 6 20
4 6 7 25
------Difference with 3rd previous rows ------
a b c
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 4.0 3.0 4.0
4 3.0 2.0 0.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.diff() method. We learned syntax, parameters, and solved examples by applying this method on the DataFrame and understood the method.