0
$\begingroup$

I have two data frames df1 and df2 which look something like this.

    Name    Age    Sex
0   Sam      25     M  
1   John     22     M
2   Larry    34     M
3   Mark     32     M

    Name    Age    Sex
0   Kelvin   51     M  
1   Bob      26     M
2   Larry    37     M
3   Jerry    17     M
6   Nick     68     M
8   Sam      98     M
9   Jax      78     M
10  John     23     M
11  Mark     47     M
12  Jerry    68     M

My output should ideally be this:

    Name    Age    Sex
0   Sam      98     M  
1   John     23     M
2   Larry    37     M
3   Mark     47     M

I have tried join and merge but the columns doesn't line up

$\endgroup$
0
$\begingroup$

Try the below code, it should do the trick for you

df1['Age'] = df1['Name'].replace(df2.set_index('Name')['Age'].dropna())
df1['Sex'] = df1['Name'].replace(df2.set_index('Name')['Sex'].dropna())
print(df1)
| improve this answer | |
$\endgroup$
0
$\begingroup$

output = df2[df2['Name'].isin(df1['Name'])

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.