1

I have three fields TEL1, TEL2, TEL3 which I want to look to see if values exist in another DF. I can do one column at a time, but was wondering if there was a quick way I could hide rows if any of the three fields contains a value in the lookup Dataframe.

I am currently using:

df1 = Sample['TEL1'].isin(DNC['NUMBER']) 

Dataframe I am looking up against:

 NUMBER 0 555 

Dataframe I am looking up:

 TEL1 TEL2 TEL3 NAME 0 222 555 555 BEN 1 222 555 222 BEN 2 555 222 555 BEN 3 222 222 222 BEN 4 555 555 555 BEN 

Expected result (row that does not contain 555 in TEL1-TEL3 columns):

 TEL1 TEL2 TEL3 NAME 3 222 222 222 BEN 

Thank you in advance, Ben.

1 Answer 1

1

You can us isin on the entire dataframe (or with the columns you are interested in) and then check which rows are all False (ie, not have any).

ix = ~df.loc[:, 'TEL1':'TEL3'].isin(DNC['NUMBER'].values).any(axis=1) df.loc[ix] 

Notice I used the values of DNC['NUMBER'] because it will try to match the index otherwise. So it'll look for 555 in the dataframe where index is 0 if you keep it as a series.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much this is perfect. The numbers will change from just 555, so it's great that it can still be dynamic.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.