3

i have 2 dataframe as below:

   student_name student_id   
1  may          0000  
2  june         1111  
3  july         2222 

  member_id member_name school_name
1 A0        april       MIT
2 B0        may         NIT
3 C0        june        LIT

i want to join the 2 dataframe in a way to produce result as below.

student_name student_id member_id member_name school_name
may          0000       B0        may         NIT
june         1111       C0        june        LIT

i am think in sql way where student_name = member_name. but i hardly able to do it in pandas.

i have try pandas merge which can base on one same name column. can you teach me a simple method to make the required results.

thank you.

1 Answer 1

9

Use merge and pass the columns to merge on for left_param and right_param respectively:

In [27]:
df.merge(df1, left_on='student_name', right_on='member_name')

Out[27]:
  student_name  student_id member_id member_name school_name
0          may           0        B0         may         NIT
1         june        1111        C0        june         LIT
Sign up to request clarification or add additional context in comments.

2 Comments

how to do if i only need to school_name column? i am google-ing the pandas merge details, but if u can enlighten me abit more. thank you. student_name student_id school_name
Just sub-select from the merged df: merged['student_name','student_id','school_name']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.