Vu Le
Vu Le

Reputation: 73

in pandas how to iterate through two data frames based on same ID

pandas

I have two data frames and want to do a nested loop.

I want to iterate of each row from df1 and select col1 (id) and col2.

Then, it will take the ID and iterate through df2 and check if the row has the same ID and then compare column from df1 with column in df2

if col2 in df1 matches col3 in df2, it will return True and append that to the row of df1.

df1
col1    col2   col3   col3
 01      A     S      True
 02      D     F      True
 03      Z     B      False

df2
col1    col2   col3
 01      A      A
 02      B      A
 02      D      F
 02      C      D
 02      D      V
 03      X      W
 03      E      X

Upvotes: 1

Views: 734

Answers (2)

user3483203
user3483203

Reputation: 51165

Setup

a = df1[['col1', 'col2']].values
b = df2[['col1', 'col2']].values

Using broadcasting with any and all:

(a == b[:, None]).any(0).all(1)

array([ True,  True, False])

Upvotes: 1

BENY
BENY

Reputation: 323366

IIUC using tuple with isin

df1[['col1','col2']].apply(tuple,1).isin(df2[['col1','col3']].apply(tuple,1))
Out[1051]: 
0     True
1     True
2    False
dtype: bool

Upvotes: 1

Related Questions