【问题标题】:How to do Keyword matching across different dataframes in Pandas?如何在 Pandas 中跨不同数据框进行关键字匹配?
【发布时间】:2020-02-13 06:45:45
【问题描述】:

我有 2 个数据框,我需要在其中映射关键字。 输入数据(df1)如下所示:

    keyword            subtopic     
    post office        Brand        
    uspshelp uspshelp  Help         
    package delivery   Shipping     
    fed ex             Brand        
    ups fedex          Brand        
    delivery done      Shipping     
    united states      location     
    rt ups             retweet      

这是用于关键字匹配的另一个数据框(df2):

Key     Media_type  cleaned_text
910040  facebook    will take post office
409535  twitter     need help with upshelp upshelp
218658  facebook    there no section post office alabama ups fedex
218658  facebook    there no section post office alabama ups fedex
518903  twitter     cant wait see exactly ups fedex truck package
2423281 twitter     fed ex messed seedless
763587  twitter     crazy package delivery rammed car
827572  twitter     formatting idead delivery done
2404106 facebook    supoused mexico united states america
1077739 twitter     rt ups

我想根据几个条件将 df1 中的“关键字”列映射到 df2 中的“cleaned_text”列:

  1. “keyword”中的一行可以映射到“cleaned_text”中的多行(一对多关系)
  2. 它应该一起选择整个关键字,而不仅仅是单个单词。
  3. 如果“关键字”与“cleaned_Text”中的多行匹配,则应在输出数据帧 (df3) 中创建新记录

这是输出数据帧(df3)的样子:

Key     Media_type  cleaned_text                                    keyword               subtopic  
910040  facebook    will take post office                           post office           Brand 
409535  twitter     need help with upshelp upshelp                  uspshelp uspshelp     Help  
218658  facebook    there no section post office alabama ups fedex  post office           Brand 
218658  facebook    there no section post office alabama ups fedex  ups fedex             Brand 
518903  twitter     cant wait see exactly ups fedex truck package   ups fedex             Brand 
2423281 twitter     fed ex messed seedless                          fed ex messed         Brand 
763587  twitter     crazy package delivery rammed car               package delivery      Shipping  
827572  twitter     formatting idead delivery done                  delivery done         Shipping  
2404106 facebook    supoused mexico united states america           united states america location  
1077739 twitter     rt ups                                          rt ups                retweet               

【问题讨论】:

标签: python pandas dataframe text-mining keyword


【解决方案1】:

如何将您的 df1 转换为字典?然后遍历您的 df2 并搜索匹配项。这可能不是最有效的方式,但它的可读性很强

keyword_dict = {row.keyword: row.subtopic for row in df1.itertuples()}
df3_data = []
for row in df2.itertuples():
    text = row.cleaned_text
    for keyword in keyword_dict:
        if keyword in text:
            df3_row = [row.Key, row.Media_type, row.cleaned_text, keyword, keyword_dict[keyword]]
            df3_data.append(df3_row)

df3_columns = list(df2.columns) + list(df1.columns)
df3 = pd.DataFrame(df3_data, columns=df3_columns)

【讨论】:

  • 它可以完成这项工作,但我有几个疑问: 1. 为什么键为 '218658' 的行会重复?它应该只出现两次而不是四次。 2. 在关键字“2404106”和“423281”中,关键字列仅显示 2 个关键字,而不是 3 个关键字。谢谢你的回答。也请帮我解答疑惑。
  • 您不需要将数据帧转换为字典来执行此操作。
  • 您的关键字 df1 仅包含 max. 2个字。因此,您可以通过更新该数据框来解决此问题。在我的代码中,只有两次出现键“218658”,所以我不知道为什么会这样。
  • 是的,我可以通过更新数据框来解决这个问题。尽管我仍然会收到“218658”的重复行。不知道为什么。让我检查更多数据并验证它。 @chatax
  • @HS-nebula 可以不用字典分享答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2020-02-05
  • 2017-08-16
  • 1970-01-01
相关资源
最近更新 更多