Reputation: 23
I have two almost identical data frames A and B. In reality its a two data frames with 1000+ names each. I want to match name and surname from both data frames and then extract the middle name from data frame B to data frame A.
data frame A
name surname
John Doe
Tom Sawyer
Huckleberry Finn
data frame B
name middle_name surname
John `O Doe
Tom Philip Sawyer
Lilly Tomas Finn
The result i seek:
name middle name surname
John `O Doe
Tom Philip Sawyer
Upvotes: 1
Views: 270
Reputation: 20669
You can use df.merge
with parameter how='inner'
and on=['name','surname']
. To get the correct order use df.reindex
over axis 1.
df = df.merge(df1,how='inner',on=['name','surname'])
df.reindex(['name', 'middle_name', 'surname'])
name middle_name surname
0 John `O Doe
1 Tom Philip Sawyer
Upvotes: 1