Skip to content
Advertisement

select non-NaN rows with multiple conditions from a pandas dataframe

Assume there is a dataframe such as

JavaScript

I would like to select non-NaN rows based on multiple conditions such as (1) col1 < 4 and (2) non-nan in col2. The following is my code but I have no idea why I did not get the 1st two rows. Any idea? Thanks

JavaScript

Advertisement

Answer

Because of the operator precedence (bitwise operators, e.g. &, have higher precedence than comparison operators, e.g. <).

Currently, your mask is being evaluated as

JavaScript

That is why no rows are being selected. You have to wrap the first condition inside parentheses

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement