Yuri Brovman
Yuri Brovman

Reputation: 1113

Filter pandas dataframe using multiple conditions defined in list

I have pandas DataFrame that I would like to filter based on multiple conditions. The conditions are passed in as a list with variable length.

Here is basic code to filter a DataFrame:

>>>> conditions = ["a","b"]    
>>>> df1 = pd.DataFrame(["a","a","b","b","c"])
>>>> df1
   0
0  a
1  a
2  b
3  b
4  c
>>>> df2 = df1[(df1[0] == conditions[0]) | (df1[0] == conditions[1])]
>>>> df2
   0
0  a
1  a
2  b
3  b

If I do not know the number of conditions passed in, is there a simple way to check for all of them?

Thank you

Upvotes: 1

Views: 4550

Answers (2)

root
root

Reputation: 33793

You can use DataFrame.isin().

df2 = df1[df1[0].isin(conditions)]

Upvotes: 12

unutbu
unutbu

Reputation: 879291

NumPy ufuncs, like np.logical_or, have a reduce method:

import numpy as np
mask = np.logical_or.reduce([(df1[0] == cond) for cond in conditions])
df2 = df1[mask]

Upvotes: 5

Related Questions