python pandas loc - filter for list of values

jeangelj picture jeangelj · Aug 21, 2017 · Viewed 79.2k times · Source

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?

Answer

taras picture taras · Aug 21, 2017

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.