Reason for Error

Can only compare identically-labeled series objects: It is Value Error, occurred when we compare 2 different DataFrames (Pandas 2-D Data Structure). If we compare DataFrames which are having different labels or indexes then this error can be thrown.

How to Reproduce the Error

Python3




# import necessary packages
import pandas as pd
 
# create 2 dataframes with different indexes
hostelCandidates1 = pd.DataFrame({'Height in CMs': [150, 170, 160],
                                  'Weight in KGs': [70, 55, 60]},
                                 index=[1, 2, 3])
 
hostelCandidates2 = pd.DataFrame({'Height in CMs': [150, 170, 160],
                                  'Weight in KGs': [70, 55, 60]},
                                 index=['A', 'B', 'C'])
 
# displaying 2 dataframes
print(hostelCandidates1)
print(hostelCandidates2)
 
# compare 2 dataframes
hostelCandidates1 == hostelCandidates2


Output:

Even though the data in the 2 DataFrames are the same but the indexes of these are different. So in order to compare the data of 2 DataFrames are the same or not, we need to follow the below approaches/solutions

How to Fix: Can only compare identically-labeled series objects

In this article, we are going to see how to fix it: Can only compare identically-labeled series objects in Python.

Similar Reads

Reason for Error

Can only compare identically-labeled series objects: It is Value Error, occurred when we compare 2 different DataFrames (Pandas 2-D Data Structure). If we compare DataFrames which are having different labels or indexes then this error can be thrown....

Method 1: With consideration of indexes

...

Method 2: Without consideration of indexes

Here we compare data along with index labels between DataFrames to specify whether they are the same or not. So instead of ‘==’ use equals method while the comparison....

Contact Us