小能豆

Pandas 查找两个数据框之间的共同匹配

py

我有 2 个数据框,我想根据列 (tld) 找到常见匹配项,一旦找到匹配项,我想将列匹配项更新为 True。如何更新目标数据框中的列?

Dataframe 1: source

   uuid           website company_name           tld
0     1  www.facebook.com     facebook  facebook.com
1     2     www.yahoo.com    yahoo inc     yahoo.com
2     3    www.google.com       Google    google.com
3     4     www.cisco.com        Cisco     cisco.com

Dataframe 2: destination

  id  website           company_name           tld  match
0  a  www.facebook.com      facebook  facebook.com  False
1  b         www.y.com     Yahoo Inc         y.com  False
2  c         www.g.com        Google         g.com  False
3  d         www.g.com    Google Inc         g.com  False
4  e  www.facebook.com  Facebook Inc  facebook.com  False

Find matches:

matches = source[source.tld.isin(destination.tld.values)]

火柴

0     1  www.facebook.com     facebook  facebook.com

Destination

  id  website           company_name           tld  match
0  a  www.facebook.com      facebook  facebook.com  True
1  b         www.y.com     Yahoo Inc         y.com  False
2  c         www.g.com        Google         g.com  False
3  d         www.g.com    Google Inc         g.com  False
4  e  www.facebook.com  Facebook Inc  facebook.com  True

阅读 17

收藏
2024-12-12

共1个答案

小能豆

使用isin更新:

df2.loc[df2.tld.isin(df1.tld),'match']=True
df2

Out[669]: 
  id           website company_name           tld  match
0  a  www.facebook.com     facebook  facebook.com   True
1  b         www.y.com     YahooInc         y.com  False
2  c         www.g.com       Google         g.com  False
3  d         www.g.com    GoogleInc         g.com  False
4  e  www.facebook.com  FacebookInc  facebook.com   True
2024-12-12