How to compare two DataFrames and output their differences side-by-side?

Given two Pandas DataFrames, we have to compare them and output their differences side-by-side.
Submitted by Pranit Sharma, on June 11, 2022

Pandas is a special tool which allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structure in pandas. DataFrames consists of rows, columns and the data. The Data inside the DataFrame can be of any type.

Sometimes we deal with multiple DataFrames which can be almost similar with very slight changes, in that case, we might need to observe the differences between the DataFrames.

Here, we are going to learn how to compare two DataFrames? For this purpose, we will check both the DataFrames if they are equal or not. To check if the DataFrames are equal or not, we will use method. Method

This method is used to compare two DataFrames and to find the differences between the rows of two DataFrames. It returns the particular column where it finds the difference.


# or

Here, a DataFrame will call this function and another DataFrames will be passed as parameter.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

# Importing pandas package
import pandas as pd

# Creating two dictionary
d1 = {

d2 = {

# Creating two separate DataFrames
df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)

# Display DataFrames

# Comparing the two DataFrames
check =

# Display check
print("Diffrence in rows of DataFrames:\n",check)


Example: Compare two DataFrames

Python Pandas Programs »


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.