How to filter DataFrame rows by list of conditions simultaneously on multiple columns

Viewed 160

I have a DataFrame where I want to filter rows appling a list of condition applicable simultaneously on two columns. For example:

pd.DataFrame({'A':['Y','Y','Y','Z','Z'],'B':[1,2,2,3,4],'C':[100,200,234,358,4243]})
   A  B     C
0  Y  1   100
1  Y  2   200
2  Y  2   234
3  Z  3   358
4  Z  4  4243
filter_on=[('Y',2),('Z',4)]

When filtering where A and B have any of the couple of values at filter_on , I should get following DataFrame:

   A  B     C
1  Y  2   200
2  Y  2   234
4  Z  4  4243
4 Answers

Try with isin after convert to tuple

out = df[df[['A','B']].apply(tuple,1).isin(filter_on)].copy()
   A  B     C
1  Y  2   200
2  Y  2   234
4  Z  4  4243

You could build a MultiIndex from the columns and use isin to compute the mask, as follows:

mask = pd.MultiIndex.from_arrays([df['A'], df['B']]).isin(filter_on)
print(df[mask])

Output

   A  B     C
1  Y  2   200
2  Y  2   234
4  Z  4  4243
data.iloc[[i for  i,(a,b) in enumerate(zip(data.A, data.B)) if (a,b) in filter_on]]

How about this succinct set_index and Index.isin based solution?

df[df.set_index(['A', 'B']).index.isin(filter_on)]

   A  B     C
1  Y  2   200
2  Y  2   234
4  Z  4  4243
Related