Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter out rows of panda-df by comparing to list [duplicate]

Tags:

python

pandas

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?

like image 850
N08 Avatar asked Jun 05 '18 14:06

N08


1 Answers

You can use isin method.

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

Output

   col1 col2
3     4    C
6     7    D
like image 183
Mihai Alexandru-Ionut Avatar answered Nov 01 '22 12:11

Mihai Alexandru-Ionut