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


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.

Assumptions

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

# SyntaxError: invalid syntax
df[!df.id.str.endswith('e')]
# ValueError: The truth value of a Series is ambiguous. 
# Use a.empty, a.bool(), a.item(), a.any() or a.all().
df[not df.id.str.endswith('e')]

Not filter using the tilde ~

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

df[~df.id.str.endswith('e')]