Python 如何在Pandas中跨不同的数据帧进行关键字匹配?

Python 如何在Pandas中跨不同的数据帧进行关键字匹配?,python,pandas,dataframe,text-mining,keyword,Python,Pandas,Dataframe,Text Mining,Keyword,我有两个数据帧,我需要在其中映射关键字。 输入数据(df1)如下所示: keyword subtopic post office Brand uspshelp uspshelp Help package delivery Shipping fed ex Brand ups fedex Brand

我有两个数据帧,我需要在其中映射关键字。 输入数据(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中的“已清理文本”列:

  • “关键字”中的一行可以映射到“已清理文本”(一对多关系)中的多行
  • 它应该一起选择整个关键字,而不仅仅是单个单词
  • 如果“关键字”与“已清理文本”中的多行匹配,则应在输出数据框(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               
    

    把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最多只包含2个单词。因此,您可以通过更新该数据帧来修复此问题。在我的代码中,键“218658”只出现了两次,所以我不知道为什么会发生这种情况。是的,我能够通过更新数据帧来解决这一问题。尽管我仍然得到“218658”的重复行。不知道为什么。让我用更多的数据来核实一下@chatax@HS-星云你能在没有字典的情况下分享答案吗?