如何检查两个数据框(熊猫)中多个列的列值?

时间:2020-10-02 05:50:01

标签: python pandas data-science

DataFrame 1:

     Email            Mobile    
  0  test1@test1.com  98989892     
  1  test4@test4.com  98989895
  2  test5@test5.com  98989894 
  3  Otheruser@mail.com 98438348342343

DataFrame 2:

     Name        Email1           Email2          Email 3    
  0  x_person    test1@test1.com  hello@hello.com Hello@Hello.com    
  1  y_person    test4@test4.com                  test2@test2.com
  2  z_person.   test5@test5.com  asasas@asas.com 

检查数据帧2的任何电子邮件列中是否存在数据帧1的“电子邮件”列值的最佳方法是什么?如果存在,那么我想将记录合并(左联接)作为匹配项。

预期结果:

 Email               Mobile                Name       Email1    
 test1@test1.com     98989892              x_person   test1@test1.com 
 test4@test4.com     98989895              y_person.  test4@test4.com  
 test5@test5.com     98989894              z_person.  test5@test5.com 
 Otheruser@mail.com  98438348342343

1 个答案:

答案 0 :(得分:1)

我认为您需要DataFrame.meltDataFrame.merge并退出联接:

df22 = df2.melt('Name', value_name='Email').drop('variable', axis=1)
df = df1.merge(df22, on='Email', how='left')
print (df)
                Email          Mobile      Name
0     test1@test1.com        98989892  x_person
1     test4@test4.com        98989895  y_person
2     test5@test5.com        98989894  z_person
3  Otheruser@mail.com  98438348342343       NaN
相关问题