pandas基于列中的相同值合并数据帧

时间:2021-05-20 22:57:49

i have 2 dataframe as below:

我有2个数据帧如下:

   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.

我想加入2数据帧,以产生如下结果。

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.

我认为在sql方式中student_name = member_name。但我几乎无法在熊猫中做到这一点。

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.

我已经尝试了pandas merge,它可以基于一个同名列。你能教我一个简单的方法来取得所需的结果吗?

thank you.

谢谢。

1 个解决方案

#1


7  

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

使用merge并传递列以分别为left_param和right_param合并:

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

#1


7  

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

使用merge并传递列以分别为left_param和right_param合并:

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