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?
df[~df['col2'].isin(['A','B'])]