I'm trying to do something like
df.query("'column' == 'a'").count()
but with
df.query("'column' == False").count()
What is the right way of using query
with a bool column?
It's simply 'column == False'
.
>>> df = pd.DataFrame([[False, 1], [True, 2], [False, 3]], columns=['column', 'another_column'])
>>> df
column another_column
0 False 1
1 True 2
2 False 3
>>> df.query('column == False')
column another_column
0 False 1
2 False 3
>>> df.query('column == False').count()
column 2
another_column 2
dtype: int64
Personally, I prefer boolean indexing (if applicable to your situation).
>>> df[~df['column']]
column another_column
0 False 1
2 False 3
>>> df[~df['column']].count()
column 2
another_column 2
dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With