. Advertisement .
..3..
. Advertisement .
..4..
I get an error:
Can only compare identically-labeled DataFrame objects
when I try to run the following code:
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 first two match exactly:
74357 True
74356 True
Name: Customer Number, dtype: bool
74357 True
74356 True
Name: Product, dtype: bool
How to fix the valueerror: can only compare identically-labeled series objects. Please give me some good ideas.
The cause:
This error happens because you are comparing DataFrames with different indexes or labels. Below is an example to illustrate it (this example only applys for DataFrames, not for Series, up to the time of Pandas 0.19 it can apply for both of them).
Solution:
To solve this error, first you need to classify the index (Remember: some functions ask classified indexes):
Note that you have to use
sort_index(axis=1)
because==
is easily affected by the order of columns:However, this error can be still raised (if the indexes or columns aren’t labelled identically after classifying or sorting).
If you don’t need to compare, you can also drop the index column.
This is how I used the same technique to test units.