I have two dataframes, dataframe A
, and its son dataframe B
, since I need to change some values in B according to A using matching columns to input the comparison result in dataframe B.
A:
name id
321 1
18 1
234 2
12 2
234 2
... ...
B:
name id matching
321 2
14 2
13 2
234 1
235 1
My searching rule is if a cell in column name of df B
(eg. name = 321) has the same name as A, then check the id of the name (eg. id = 1 in B, id = 2 in A).
After that, label all items who have the same id in B (eg. in B, name = 321,14,13 all have id =2) as matching = 1.
I want the output like:
B:
name id matching
321 2 1
14 2 1
13 2 1
234 1 2
235 7 2
My code takes a long time to work and still does not achieve my goal:
for i in range(0,len(A)):
for j in range(0,len(B)):
if A.name[i]== B.name[j]:
B.matching[j] = A.id[i]
I want the output like:
OutB:
name id matching
321 2 1
14 2 1
13 2 1
234 1 2
235 7 2
Is there any function or algorithm that could help me complete my goal?