繁体   English   中英

如何检查两个数据帧(Pandas)中多列中的列值?

[英]How to check column value in multiple columns in two dataframes (Pandas)?

数据帧 1:

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

数据帧 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 

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

预期结果:

 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

我认为你需要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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM