The following problem occurs often. Say I have a dataframe, where one column can take a discrete value:
df = pd.DataFrame({'col1': [1, 2,3,4,5,6,7], 'col2': ["A", "B", "A", "C", "B", "A", "D"]})
In this case col2
can take values A, B or C. I only want to rows where col2
is not equal to A or B. I thought the following syntax would work,
df["col2"] not in ["A", "B"]
However, this gives me the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Is there a neat way to filter those rows out?
You can use isin
method.
df = df[~df.col2.isin(['A', 'B'])]
Output
col1 col2
3 4 C
6 7 D
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