2

I am able to filter a dataframe based on the elements of a list like this;

import pandas as pd
W1 = ['Animal','Ball','Cat','Derry','Element','Lapse','Animate this']
W2 = ['Krota','Catch','Yankee','Global','Zeb','Rat','Try']
df = pd.DataFrame({'W1':W1,'W2':W2})

l1 = ['Animal','Zeb','Q']
print df[df['W1'].isin(l1) | df['W2'].isin(l1)]

        W1     W2
  0   Animal  Krota
  4  Element    Zeb

But is there a way to filter by applying regular expressions; For ex;

 l1 = ['An','Cat']

 Intended result;
          W1         W2
  0   Animal        Krota
  1   Ball          Catch  
  2   Cat           Yankee
  6   Animate this  Try 

1 Answer 1

6

Try this:

df[df['W1'].str.contains("|".join(l1)) | df['W2'].str.contains("|".join(l1))]


             W1      W2
0        Animal   Krota
1          Ball   Catch
2           Cat  Yankee
6  Animate this     Try
Sign up to request clarification or add additional context in comments.

2 Comments

How would you modify this, if l1= pd.DataFrame({'W1': ['An', 'Ze'] 'W2': ['Kr', 'El']})? Here An, Ze, Kr, El` are regex values. Can the entries in a Dataframe be regex values?
I am aware, I can use isin, if l1 is a dataframe and I want perfect matches.. but I have a case where the entries in the dataframe l1 are not going to be perfect matches.. so I probably need to use regex values..

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.