Denis Dragnev
Denis Dragnev

Reputation: 23

match name and surname from two data frames, extract middle name from one data frame and append it to other

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

Answers (1)

Ch3steR
Ch3steR

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

Related Questions