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

12 Dec, 2021
In this article, we are going to see how to fix it: Can only compare identically-labeled series objects in Python.
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.
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = 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 dataframesprint(hostelCandidates1)print(hostelCandidates2) # compare 2 dataframeshostelCandidates1 == 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
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 packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = 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 dataframesprint(hostelCandidates1)print(hostelCandidates2) # compare 2 dataframeshostelCandidates1.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.
To drop indexes of DataFrame use reset_index method. By dropping the indexes, makes an easy task such that interpreters just check the data irrespective of index values.
Syntax: dataframeName.reset_index(drop=True)
There are 2 ways of comparing data:
Whole DataFrame
Row by Row
Example 1: Entire DataFrame Comparison
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = 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 dataframesprint(hostelCandidates1)print(hostelCandidates2) # compare 2 dataframeshostelCandidates1.reset_index(drop=True).equals(    hostelCandidates2.reset_index(drop=True))
Output:
Here the data is the same and even though the indexes are different we are comparing the DataFrames by eliminating the index labels so it returns true.
Example 2: Row by Row Comparison
Python3
# import necessary packagesimport pandas as pd # create 2 dataframes with different indexeshostelCandidates1 = 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 dataframesprint(hostelCandidates1)print(hostelCandidates2) # compare 2 dataframeshostelCandidates1.reset_index(    drop=True) == hostelCandidates2.reset_index(drop=True)
Output:
This approach helps us to identify where there are differences between 2 DataFrames and do not compare its index labels as their index labels are dropped while comparison.
rkbhola5
varshagumber28
simranarora5sos
Picked
Python How-to-fix
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Box Plot in Python using Matplotlib
Bar Plot in Matplotlib
Python | Get dictionary keys as a list
Python | Convert set into a list
Ways to filter Pandas DataFrame by column values
Python - Call function from another file
loops in python
Multithreading in Python | Set 2 (Synchronization)
Python Dictionary keys() method
Python Lambda Functions