2

I have created multiple filters for a Dataframe:

filt1 = ~df["message"].str.contains("<Media omitted>", na=False),
filt2 = ~df["message"].str.contains("http://", na=False),
filt3 = ~df["message"].str.contains("Dropped pin", na=False), 

I can filter the dataframe using:

df[filt1 & filt2 & filt3]

But as I add more filters this seems like a stupid way to filter. How do I apply multiple filters to a dataframe?

I tried adding each filter to a list doing df[filterlist] and df[*filterlist] but these do not work.

2 Answers 2

3

You can use np.logical_and.reduce:

filterlist = [filt1, filt2, filt3]

df[np.logical_and.reduce(filterlist)]

Or concat with DataFrame.all for test all Trues per rows:

df[pd.concat(filterlist, axis=1).all(axis=1)]

If possible use | for regex or:

filt = ~df["message"].str.contains("<Media omitted>|http://|Dropped pin", na=False)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use join to merge list for regex

df[~df.message.str.contains('|'.join(filterlist))]

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.