You want to combine twice. The first merge focuses on a1the left data frame and a1the correct data frame. Second merge: you switch focus to a2from the left frame.
df1.merge(
df2,
left_on=['seq', 'a1'],
right_on=['seq', 'a1'],
how='left'
).join(
df1.merge(
df2,
left_on=['seq', 'a2'],
right_on=['seq', 'a1'],
how='left'
)[['p1', 'p2']].rename(columns=dict(p1='p3', p2='p4'))
)
seq id a1 a2 p1 p2 p3 p4
0 12 209981 None None NaN NaN NaN NaN
1 12 209982 Funds None 5.71 1.09 NaN NaN
2 13 209983 Free_Income None 2.53 2.64 NaN NaN
3 13 209984 Free_Income None 2.53 2.64 NaN NaN
4 14 209985 Free_Income Hybrid 4.53 2.25 1.89 2.45
source
share