Signup/Sign In

Pandas DataFrame equals() Method

In this tutorial, we will learn the Python pandas DataFrame.equals() method. It tests whether two objects contain the same elements. This method allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements. NaNs in the same location are considered equal. It returns a bool, True if all elements are the same in both objects, False otherwise.

  • The row/column index does not need to have the same type, as long as the values are considered equal.
  • Corresponding columns must be of the same dtype.

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

Syntax

DataFrame.equals(other)

Parameters

other: Series or DataFrame. The other Series or DataFrame to be compared with the first.

Example 1: The DataFrame.equals() Method

Here, DataFrames df1 and df2 have the same types and values for their elements and column labels, which will return True.

import pandas as pd
df1 = pd.DataFrame({"col_1": [10,20], "col_2": [20,30]})
print("----The First DataFrame is-----")
print(df1)
df2 = pd.DataFrame({"col_1": [10,20], "col_2": [20,30]})
print("----The second DataFrame is-----")
print(df2)
print("Are the elements in two DataFrame contains same elements:",df1.equals(df2))

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


----The First DataFrame is-----
col_1 col_2
0 10 20
1 20 30
----The second DataFrame is-----
col_1 col_2
0 10 20
1 20 30
Are the elements in two DataFrame contains same elements: True

Example 2: The DataFrame.equals() Method

The below example is similar to the previous example. DataFrames df1 and df2 consisting of null values and the null values in the same location are considered equal.

import pandas as pd
df1 = pd.DataFrame({"col_1": [10,None], "col_2": [20,30]})
print("----The First DataFrame is-----")
print(df1)
df2 = pd.DataFrame({"col_1": [10,None], "col_2": [20,30]})
print("----The second DataFrame is-----")
print(df2)
print("Are the elements in two DataFrame contains same elements:",df1.equals(df2))

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


----The First DataFrame is-----
col_1 col_2
0 10.0 20
1 NaN 30
----The second DataFrame is-----
col_1 col_2
0 10.0 20
1 NaN 30
Are the elements in two DataFrame contains same elements: True

Example 3: The DataFrame.equals() Method

DataFrames df1 and df2 have different types for the same values for their elements, and will return False even though their column labels are the same values and types.

import pandas as pd
df1 = pd.DataFrame({"col_1": [10.0,20.0], "col_2": [20.0,30.0]})
print("----The First DataFrame is-----")
print(df1)
df2 = pd.DataFrame({"col_1": [10,20], "col_2": [20,30]})
print("----The second DataFrame is-----")
print(df2)
print("Are the elements in two DataFrame contains same elements:",df1.equals(df2))

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


----The First DataFrame is-----
col_1 col_2
0 10.0 20.0
1 20.0 30.0
----The second DataFrame is-----
col_1 col_2
0 10 20
1 20 30
Are the elements in two DataFrame contains same elements: False

Conclusion

In this tutorial, we learned the Python pandas DataFrame.equals() method. We solved different examples by applying this method to the two different DataFrames.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.