How to Use not (!) in Pandas DataFrame Filtering Logic

Published Nov 28, 2021  ∙  Updated Jan 3, 2022

How can we apply the not boolean operator on a condition when filtering a Pandas DataFrame?

Suppose we want all rows in the id column that don’t end in e.


We might think to use the exclamation point ! or the not operator, but these conditions yield some errors.

# SyntaxError: invalid syntax
# ValueError: The truth value of a Series is ambiguous. 
# Use a.empty, a.bool(), a.item(), a.any() or a.all().

Not filter using the tilde ~

We can use the tilde ~ instead of ! or not to negate the conditional in the Pandas DataFrame filtering logic.