sectechguy
sectechguy

Reputation: 2117

Pandas find every row in df1 where df2 matches either column

I have 2 pandas dataframes. I would like to use the values in df2 and find the full row in df1 where it matches.

df1

   col1   col2
0  55     66
1  12     25 
2  18     22 
3  52     15
4  15     30
5  16     17
6  17     30
7  31     20

df2

   duplicates
0  15
1  17

I want to use the values in df2 and iterate through df1 and recieve and output like the following:

df3

   col1   col2
3  52     15
4  15     30
5  16     17
6  17     30



Upvotes: 3

Views: 126

Answers (2)

yatu
yatu

Reputation: 88305

You could use isin with any to check if any columns in a given row are True:

df1[df1.isin(df2.duplicates.values).any(1)]

    col1  col2
3    52    15
4    15    30
5    16    17
6    17    30

Upvotes: 2

BENY
BENY

Reputation: 323396

Using isin with any

df1[df1.isin(df2.duplicates.values).any(1)]
Out[26]: 
   col1  col2
3    52    15
4    15    30
5    16    17
6    17    30

Upvotes: 2

Related Questions