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


One error you may encounter when using pandas is:

ValueError: Can only compare identically-labeled DataFrame objects

This error occurs when you attempt to compare two pandas DataFrames and either the index labels or the column labels do not perfectly match.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we have the following two pandas DataFrames:

import pandas as pd

#define DataFrames
df1 = pd.DataFrame({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

df2 = pd.DataFrame({'points': [25, 12, 15, 14],
                    'assists': [5, 7, 13, 12]},
                     index=[3, 2, 1, 0])

#view DataFrames
print(df1)

   points  assists
0      25        5
1      12        7
2      15       13
3      14       12

print(df2)

   points  assists
3      25        5
2      12        7
1      15       13
0      14       12

Notice that the column labels match, but the index labels do not.

If we attempt to compare the two DataFrames, we’ll receive an error:

#attempt to compare the DataFrames
df1 = df2

ValueError: Can only compare identically-labeled DataFrame objects

How to Fix the Error

There are a few methods we can use to address this error.

Method 1: Compare DataFrames (including index labels)

We can use the following syntax to compare the two DataFrames to see if they perfectly match (including the index labels):

df1.equals(df2)

False

This tells us that the two DataFrames do not perfectly match (including the index labels).

Method 2: Compare DataFrames (ignore index labels)

We can use the following syntax to compare the two DataFrames to see if they perfectly match, while completely ignoring the index labels:

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

True

This tells us that the two DataFrames perfectly match (not accounting for the index labels).

Method 3: Compare DataFrames Row by Row

We can use the following syntax to compare the two DataFrames row by row to see which row values match:

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

      points	assists
0	True	   True
1	True	   True
2	True	   True
3	True	   True

This allows us to see which values match in each row.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

Leave a Reply

Your email address will not be published.