Sign Up

Have an account? Sign In Now

Sign In

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

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 605
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 8
Kenil Vasani
Asked: December 14, 20202020-12-14T21:06:52+00:00 2020-12-14T21:06:52+00:00In: Python

Error“Can only compare identically-labeled Series objects” and sort_index

  • 8

I have two dataframes df1 df2with the same numbers of rows and columns and variables, and I’m trying to compare the boolean variable choice in the two dataframes. Then use if/else to manipulate the data. But something seems wrong when I try to compare the boolean var.

Here are my dataframes sample and codes:

#df1
v_100     choice #boolean
7          True
0          True
7          False
2          True

#df2
v_100     choice #boolean
1          False
2          True
74         True
6          True

def lastTwoTrials_outcome():
     df1 = df.iloc[5::6, :] #df1 and df2 are extracted from the same dataframe first
     df2 = df.iloc[4::6, :]

     if df1['choice'] != df2['choice']:  # if "choice" is different in the two dataframes
         df1['v_100'] = (df1['choice'] + df2['choice']) * 0.5

Here’s the error:

if df1['choice'] != df2['choice']:
File "path", line 818, in wrapper
raise ValueError(msg)
ValueError: Can only compare identically-labeled Series objects

I found the same error here, and an answer suggests to sort_index first, but I don’t really understand why though? Can anyone explain more in detail please (if that’s the correct solution)?

Thanks!

booleanindexingpandaspython
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      I think you need reset_index for same index values and then comapare – for create new column is better use mask or numpy.where:

      Also instead + use | because working with booleans.

      df1 = df1.reset_index(drop=True)
      df2 = df2.reset_index(drop=True)
      df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
                                        (df1['choice'] + df2['choice']) * 0.5)
      
      
      df1['v_100'] = np.where(df1['choice'] != df2['choice'],
                             (df1['choice'] | df2['choice']) * 0.5,
                              df1['choice'])
      

      Samples:

      print (df1)
         v_100  choice
      5      7    True
      6      0    True
      7      7   False
      8      2    True
      
      print (df2)
         v_100  choice
      4      1   False
      5      2    True
      6     74    True
      7      6    True
      

      df1 = df1.reset_index(drop=True)
      df2 = df2.reset_index(drop=True)
      print (df1)
         v_100  choice
      0      7    True
      1      0    True
      2      7   False
      3      2    True
      
      print (df2)
         v_100  choice
      0      1   False
      1      2    True
      2     74    True
      3      6    True
      
      df1['v_100'] = df1['choice'].mask(df1['choice'] != df2['choice'],
                                        (df1['choice'] | df2['choice']) * 0.5)
      
      print (df1)
         v_100  choice
      0    0.5    True
      1    1.0    True
      2    0.5   False
      3    1.0    True
      
      • 8
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      create-react-app is not working since version 4.0.1

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com