CIsForCookies
CIsForCookies

Reputation: 12817

how to correctly check if dataframe column has "False" in it

I have a dataframe with boolean values. If any of the values is False, I want to do something. I have 2 options to do so:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame(data = [{'bool': False}, {'bool': True}])

In [3]: df
Out[3]: 
    bool
0  False
1   True

In [4]: if (df['bool'] is False).any():
   ...:     print ('yay!!!!')
   ...: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-0c98c5d85cb6> in <module>
----> 1 if (df['bool'] is False).any():
      2     print ('yay!!!!')
      3 

AttributeError: 'bool' object has no attribute 'any'

In [5]: if (df['bool'] == False).any():
   ...:     print ('yay!!!!')
   ...: 
yay!!!!

As you can see, the 1st (is) fails, and the 2nd (==) succeeds. However, when running pre-commit (version 1.20.0) on this, I get the following error:

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

which reverts me back to the 1st solution (which doesn't work)

How can I solve this?

Python 3.6.12
pandas==1.1.5

Upvotes: 1

Views: 7128

Answers (1)

jezrael
jezrael

Reputation: 862641

You can test if not all values are Trues by test Series by Series.all and then invert scalar by not:

if not df['bool'].all():
    print ('yay!!!!')
yay!!!!

If want invert Series:

if (~df['bool']).any():
    print ('yay!!!!')
yay!!!!

In pandas working with 1d (Series) or 2d (DataFrame) data, so for test is not used is , which is not used for check for identity of arrays.

Upvotes: 2

Related Questions