shashwat shukla
shashwat shukla

Reputation: 15

Merging three data frames with different common keys

I want to merge three data frames A,B,C as below. DF A and B have a common key(imonum), DF B and C have a common key (id).

DF A    
imonum  name
2234    xyz
3445    abc
2234    xyz
3445    abc


----------


DF B    
imonum  id
2234    1
3445    2


----------
DF C    
id  mailid
1   email(xyz)
2   email(abc)

DF required     
imonum  name    mailid
2234    xyz email(xyz)
3445    abc email(abc)
2234    xyz email(xyz)
3445    abc email(abc)

How can i get the required data frame

Upvotes: 1

Views: 32

Answers (1)

Zero
Zero

Reputation: 1899

This does the job,

merge_1 = pd.merge(A, B)
merge_2 = pd.merge(B, C)

final_merge = pd.merge(merge_1, merge_2)

Output -

imonum name id mailid
0 2234 xyz 1 email(xyz)
1 2234 xyz 1 email(xyz)
2 3445 abc 2 email(abc)
3 3445 abc 2 email(abc)

Upvotes: 1

Related Questions