Pandas Boolean question

Hello,

I don’t understand why my code returns an answer minus one row [second last row of the “expected”]. Clearly the country column has the value Venezuela but it doesn’t seem to pick it up when I run it. Am I doing something wrong?

Your first line is wrong. It should be like this:

brazil_venezuela = f500[(f500['country'] == 'Brazil') | (f500['country'] == 'Venezuela')]

When you have more than one condition in a boolean mask you must have each of them between parentheses and they need to be connected by | (which means “or”) or & (which means “and”).

Hope this helps you.

Hi Otavios,

Just another question from your answer: I’m wondering why my code returned with an incomplete data rather than returning a syntax error since the requirement for boolean was not met.

That is because or is valid python operator. If you run print('Brazil' or 'Venezuela') for example, Python you return the first element, as it returns the first truthy value when dealing non-boolean values. You can see more abou it in this stackoverflow quesiton.

However, pandas only recognizes | an & d as logical operators. So in your code, python will first solve the logical operation ('Brazil' or 'Venezuela') and only then will work on f500['country'] == and as the ('Brazil' or 'Venezuela') returns Brazil your code is the same as:

brazil_venezuela = f500[(f500['country'] == 'Brazil')]

that makes so much sense. Thanks for helping out :slight_smile:

1 Like