Ben Swann
Ben Swann

Reputation: 133

Applying .isin for multiple series in a DF

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.

Upvotes: 1

Views: 143

Answers (1)

busybear
busybear

Reputation: 10590

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.

Upvotes: 1

Related Questions