python pandas loc – filter for list of values

Question:

This should be incredibly easy, but I can’t get it to work.

I want to filter my dataset on two or more values.

#this works, when I filter for one value
df.loc[df['channel'] == 'sale'] 

#if I have to filter, two separate columns, I can do this
df.loc[(df['channel'] == 'sale')&(df['type']=='A')] 

#but what if I want to filter one column by more than one value?
df.loc[df['channel'] == ('sale','fullprice')] 

Would this have to be an OR statement? I can do something like in SQL using in?

Asked By: jeangelj

||

Answers:

There is a df.isin(values) method wich tests
whether each element in the DataFrame is contained in values.
So, as @MaxU wrote in the comment, you can use

df.loc[df['channel'].isin(['sale','fullprice'])]

to filter one column by multiple values.

Answered By: taras
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.