With 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.
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.equals(hostelCandidates2) |
Output:
As the data is the same but the index labels of these 2 data frames are different so it returns false instead of an error.
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.