0

I am using a pandas dataframe and I am trying to select rows where the yearID == 2001 and the team_IDx == 'OAK'. The yearID column is of type int and team_IDx is an object. Here's the expression I'm using:

mergeddf.loc[(mergeddf['yearID'] == 2001 & mergeddf['teamID_x'] == 'OAK')]

But I keep getting the error:

TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]

I'm a beginner and not even sure how to phrase my question. I've looked at other answers on stack overflow, but they don't make sense to me. What does this error mean? What underlying concepts should I know about to be able to understand it on my own? How do I resolve this?

cs95
  • 379,657
  • 97
  • 704
  • 746

1 Answers1

1

This is due to the operator precedence of the bitwise operators, which have higher precedence than logical operators. You need another layer of parentheses around each condition:

mergeddf.loc[((mergeddf['yearID'] == 2001) & (mergeddf['teamID_x'] == 'OAK'))]
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Thank you! This fixed my problem. Do you have a resource I can use to learn more about operator precedence for the Pandas bitwise operators? – Nadaa Taiyab Jul 22 '17 at 20:58
  • @NadaaTaiyab Afraid I don't have anything better for you than the official docs and SO questions :) I started learning pandas a couple of weeks ago by shadowing other uses in the Pandas tags. – cs95 Jul 22 '17 at 20:59