(TIL) Pandas: Inverse of boolean

less than 1 minute read

In pandas, you can use the tilde (~) to flip bool values:

>>> df = pd.DataFrame({"A": ["Hello", "this", "World", "apple"]})
>>> df[df.A.str.contains("Hello|World")]
       A
0   Hello
2   World
>>> df[~df.A.str.contains("Hello|World")]
       A
1   this
3  apple

Via StackOverflow.

Tags: ,

Categories:

Updated:

Comments