简体   繁体   中英

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

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

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM