1

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

2 Answers 2

12

You can use DataFrame.isin().

df2 = df1[df1[0].isin(conditions)]
Sign up to request clarification or add additional context in comments.

Comments

5

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.