I have two Dataframes DF1
and DF2
. My goal is to look up DF2
with DF1
columns as
keys; and save the returns as outcomes in DF3
. Can someone help me with getting
DF3
?
e.g.
DF1 DF2
map test1 test2 No. outcome
A NaN NaN 1 AA
B NaN 5 2 BB
C 1 6 3 CC
D 2 7 4 DD
E 3 NaN 5 EE
F 4 NaN 6 FF
G 5 8 7 GG
H 6 9 8 HH
I 7 10 9 II
10 JJ
11 KK
12 LL
13 MM
DF3
map test1 test2 outcome1 outcome2
A NaN NaN NaN NaN
B NaN 5 NaN EE
C 1 6 AA FF
D 2 7 BB GG
E 3 NaN CC NaN
F 4 NaN DD NaN
G 5 8 EE HH
H 6 9 FF II
I 7 10 GG JJ
I am currently using two join functions, but this is not what I need. It drops NaN
s in DF1
, and only returns the overlap of test1
and test2
.
df3 = df1.merge(df2, how='inner', left_on='test1', right_on='No.')
df3 = df3.merge(df2, how='inner', left_on='test2', right_on='No.')
currently my code will return this:
DF3
map test1 test2 outcome1 outcome2
C 1 6 AA FF
D 2 7 BB GG
G 5 8 EE HH
H 6 9 FF II
I 7 10 GG JJ