8

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?

1
  • 1
    You want df[~df['col2'].isin(['A','B'])] Commented Jun 5, 2018 at 14:10

1 Answer 1

7

You can use isin method.

df = df[~df.col2.isin(['A', 'B'])] 

Output

 col1 col2 3 4 C 6 7 D 
Sign up to request clarification or add additional context in comments.

1 Comment

this is a dupe, not necessary to answer IMO, also didn't downvote

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.