多次匹配后合并熊猫数据框

时间:2018-07-10 07:09:29

标签: python pandas dataframe

我有2个数据框,在其中我根据列(tld找到了常见的匹配项,如果找到匹配项(在sourcedestination中的列之间),我复制了该值从源到uuid数据帧的列(destination)的范围。 我还要检查其他列是否匹配。 (company_name)然后提取uuid

现在,我需要比较另一列(similar_companies)并提取uuid

数据框1:来源

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

数据框2:目标

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

所需的输出:

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

  uuid  
0   1a  
1  NaN  
2   3c  
3  NaN  
4   1a  
5   1a

当前代码:

# Find if TLD is the same.
match_tld = destination.tld.isin(source.tld)
# Find if Company name is the same.
match_company_name = destination.company_name.isin(
      source.company_name)
# Find similar source.
destination[
      _SIMILAR_COMPANIES] = destination.company_name.apply(
          _FindSimilarCompanies, args=(destination,))
# Find if Company name is the same from similar source.
match_similar_companies = destination.similar_companies.isin(
      source.company_name)
# Update match column if TLD or company_name matches.
destination['match'] = match_tld | match_company_name | match_similar_companies
# Extract UUID for TLD matches.
merge_tld = destination.merge(
      source[['tld', 'uuid']], on='tld', how='left')
# Extract UUID for company name matches.
destination = destination.merge(
      source[['company_name', 'uuid']], on='company_name', how='left')
# I insert new line here!!!
# Combine dataframes.
destination['uuid'] = destination['uuid'].combine_first(merge_tld['uuid'])
logging.info(source)
logging.info(destination)

上面的代码适用于2列,但是当我尝试合并新列时,出现KeyError :(我在上面插入新代码的地方添加了注释)

destination = destination.merge(
      source[['company_name', 'uuid']], on='similar_companies', how='left')

错误:

KeyError: 'similar_companies'

1 个答案:

答案 0 :(得分:1)

我认为问题不是similar_companies中的列source,所以有必要rename

#for sample data column
_SIMILAR_COMPANIES = 'similar_companies'
destination[_SIMILAR_COMPANIES] = destination.company_name.str.extract('([fF]acebook)')

destination1 = destination.merge(
      source[['company_name', 'uuid']], on='company_name', how='left')

destination2 = (destination.merge(
       source[['company_name', 'uuid']].rename(columns={'company_name':'similar_companies'}),
       on='similar_companies', how='left'))
# Combine dataframes.
merge_tld['uuid'] = (merge_tld['uuid'].combine_first(destination1['uuid'])
                                      .combine_first(destination2['uuid']))
print (merge_tld)
  id           website  company_name           tld  match similar_companies  \
0  a  www.facebook.com      facebook  facebook.com   True          facebook   
1  b         www.y.com     Yahoo Inc         y.com  False               NaN   
2  c         www.g.com        Google         g.com   True               NaN   
3  d         www.g.com    Google Inc         g.com  False               NaN   
4  e  www.facebook.com  Facebook Inc  facebook.com   True          Facebook   

  uuid  
0   1a  
1  NaN  
2   3c  
3  NaN  
4   1a