71

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?

1
  • 9
    df.loc[df['channel'].isin(['sale','fullprice'])] Commented Aug 21, 2017 at 18:42

1 Answer 1

150

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.

Sign up to request clarification or add additional context in comments.

2 Comments

df.loc[df['channel'].apply(lambda x: x in ['sale','fullprice'])] would also work. It is not as succinct as using df.isin but can be modified to check any complitcated condition depending on just one column.
Yes, definitely.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.