Pandas “Can only compare identically-labeled DataFrame objects” error

I’ve been working with pandas to handle a data set and do some operation and analysis.

For a purpose, I have compare two datasets – especially particular fields.

I have faced the following while comparing two fields in my flow.

Can only compare identically-labeled DataFrame objects

For example, I had my code comparison like below

import pandas as pd
dataframe01 = pd.DataFrame(....)
dateframe02 = pd.DataFrame(....)
# Trying to compare and print
print(dataframe01 == dataframe02)

While attempting this approach, I have faced this issue.

From the error message, all I could to understand is labels aren’t matched.

Solution

After referring some docs or online content, I have tried the following approach to compare the data.

print(dataframe01.equals(dataframe02))

This will help us to check whether both the data frames are perfectly matching or not.

There are other options to ignore index labels as well, you can use it based on your needs.

Thanks for reading!


Related post: How to suppress Pandas Future warning ?

Advertisement

One thought on “Pandas “Can only compare identically-labeled DataFrame objects” error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s