Daniel Aparisi
Daniel Aparisi

Reputation: 1

How do parenthesis affect filters in Pandas?

I am learning Pandas and while playing around with some filters something came up.

I was using this filter on the classical stackoverflow survay to get only the rows where the applicants had an even Respondent ID AND they were coding as a hobby:

filt = df["Hobbyist"] == "Yes" & df["Respondent"] % 2 == 0

But then the "Yes" got higlighted by the code editor and I got the following error:

TypeError: Cannot perform 'rand_' with a dtyped [int64] array and scalar of type [bool]

That's when I put parenthesis around the (df["Hobbyist"] == "Yes"), but then, the only filter that was apply was the Respondent one. Even with the & operator.

At the end I added parenthesis for both conditions and it worked fine but I felt curious about the reason why it worked only the Respondent filter.

Upvotes: 0

Views: 461

Answers (1)

mozway
mozway

Reputation: 260835

This is due to operator precedence.

& has higher precedence than ==, thus your initial code is equivalent to:

filt = df["Hobbyist"] == ("Yes" & (df["Respondent"] % 2)) == 0

You should use:

filt = (df["Hobbyist"] == "Yes") & (df["Respondent"] % 2 == 0)

Or, to avoid parentheses:

filt = df["Hobbyist"].eq("Yes") & df["Respondent"].mod(2).eq(0)

Upvotes: 2

Related Questions