24

I'm filtering my DataFrame dropping those rows in which the cell value of a specific column is None.

df = df[df['my_col'].isnull() == False]

Works fine, but PyCharm tells me:

PEP8: comparison to False should be 'if cond is False:' or 'if not cond:'

But I wonder how I should apply this to my use-case? Using 'not ...' or ' is False' did not work. My current solution is:

df = df[df['my_col'].notnull()]
10
  • 7
    df = df[df['my_col'].notnull()] ? Commented Apr 5, 2018 at 13:16
  • 1
    ~ is the not operator Commented Apr 5, 2018 at 13:17
  • 1
    Still I wonder how this is related to the PEP8 message. Commented Apr 5, 2018 at 13:18
  • 1
    @MohammadAthar that doesn't work. As mentioned in my question. Commented Apr 5, 2018 at 13:19
  • 4
    df = df.dropna(subset=['my_col']) Commented Apr 5, 2018 at 13:20

1 Answer 1

37

So python has the short-circuiting logic operators not, and, or. These have a very specific meaning in python and cannot be overridden (not must return a bool and a and/or b always returns either a or b or throws an error.

However, python also has over-loadable boolean operators ~ (not), & (and), | (or) and ^ (xor).

You may recognise these as the int bitwise operators, but Numpy (and therefore pandas) use these to do array / series boolean operations.

For example

b = np.array([True, False, True]) & np.array([True, False, False])
# b --> [True False False]
b = ~b 
# b --> [False True True]

Hence what you want is

df = df[~df['my_col'].isnull()]

I agree with PEP8, don't do == False.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explantation and yet another example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.