Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The complete guide to the valueerror: can only compare identically-labeled series objects issue.
Next
Answered
Amani Lam
  • 8
Amani Lam
Asked: May 18, 20222022-05-18T20:33:16+00:00 2022-05-18T20:33:16+00:00In: python

The complete guide to the valueerror: can only compare identically-labeled series objects issue.

  • 8

. 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.

can only compare identically-labeled series objects
  • 2 2 Answers
  • 212 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-27T09:21:49+00:00Added an answer on June 27, 2022 at 9:21 am

    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).

    In [1]: df1 = pd.DataFrame([[1, 2], [3, 4]])
    
    In [2]: df2 = pd.DataFrame([[3, 4], [1, 2]], index=[1, 0])
    
    In [3]: df1 == df2
    Exception: Can only compare identically-labeled DataFrame objects

    Solution:

    To solve this error, first you need to classify the index (Remember: some functions ask classified indexes):

    In [4]: df2.sort_index(inplace=True)
    
    In [5]: df1 == df2
    Out[5]:
    0 1
    0 True True
    1 True True

    Note that you have to use sort_index(axis=1) because == is  easily affected by the order of columns:

    In [11]: df1.sort_index().sort_index(axis=1) == df2.sort_index().sort_index(axis=1)
    Out[11]:
    0 1
    0 True True
    1 True True

    However, this error can be still raised (if the indexes or columns aren’t labelled identically after classifying or sorting).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Chloë Grand
    2022-05-25T21:06:15+00:00Added an answer on May 25, 2022 at 9:06 pm

    If you don’t need to compare, you can also drop the index column.

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

    This is how I used the same technique to test units.

    from pandas.util.testing import assert_frame_equal
    
    assert_frame_equal(actual.reset_index(drop=True), expected.reset_index(drop=True))
    • 22
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.