valueerror: can only compare identically-labeled

Cause of valueerror: can only compare identically-labeled dataframe objects

This error of valueerror: can only compare identically-labeled dataframe objects occurs while working with the Pandas module in python. One can get this error while using data frame objects in the Pandas module. This error comes while caused by mparing two data frame objects. The code for the following is:

 

uat = uat[['Customer Number','Product']]
prod = prod[['Customer Number','Product']]
print uat['Customer Number'] == prod['Customer Number']
print uat['Product'] == prod['Product']
print uat == prod

 

The output for the above code is:

 

 

The above code will provide an error in the third print statement. 

Solution

In the above code when can get an error while running the last print statement. The error will look like as shown below:

 

Exception: Can only compare identically-labeled DataFrame objects

 

The above problem can be solved by using the sort method. 

df2.sort_index(inplace=True)
df1 == df2

Since == is also dependent on the arrangement of the columns, sort index(axis=1) can be necessary:

 

df1.sort_index().sort_index(axis=1) == df2.sort_index().sort_index(axis=1)

 

If the index column is not required for comparison, one might alternatively try removing it:

 

print(df1.reset_index(drop=True) == df2.reset_index(drop=True))

 

As a last solution, one can use the equal function of Pandas library to get the desired result.

 

df1.equals(df2) 

 

Some variations from == include:

  • You don’t experience the issue that the question describes.
  • It gives back just a boolean.
  • NaN values are treated equally if they appear in the same place.
  • For two DataFrames to be regarded as equal, their dtypes must match.

 

 

Also Read: What is IndexError and how to solve it?

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *