【问题标题】:How do I extract multiple keywords on a certain descriptions如何在某个描述上提取多个关键字
【发布时间】:2019-01-28 05:36:57
【问题描述】:

这是我的数据集

No   Description
1    Paying Google ads
2    Purchasing Facebook Ads
3    Purchasing Ads
4    AirBnB repayment

我有 txt 文件名为 entity.txt

0, Google
1, Facebook
2, Ads

我需要在我的数据框中检测entity.txt 上的所有关键字,无论是单个关键字还是多个关键字,如果没有检测到一个关键字,我们称之为Other,所以我的输出期望是:

No   Description                 Keyword
1    Paying Google ads           Google
2    Purchasing Facebook Ads     Facebook Ads
3    Purchasing LinkedIn Ads     LinkedIn Ads
4    AirBnB repayment            Other

这就是我所做的

with open('entity.txt') as f: 
    content = f.readlines()
content = [x.strip() for x in content ]
df['keyword'] = df['description'].apply(lambda x: ' '.join([i for i in content if i in x]))
df['keyword'] = df['keyword'].replace('', 'Other')

但是,结果是

No   Description                 Keyword
1    Paying Google ads           Other
2    Purchasing Facebook Ads     Other
3    Purchasing LinkedIn Ads     Other
4    AirBnB repayment            Other

【问题讨论】:

    标签: pandas dataframe nlp


    【解决方案1】:

    使用str.findalldf1 中的所有值提取到列表中,然后将空列表转换为Other,所有填充的列表通过空格与str.join 连接:

    df1 = pd.DataFrame({'entity':['Google','Facebook','Ads']})
    
    s = df['Description'].str.findall(r'({})'.format('|'.join(df1['entity'])))
    df['Keyword'] = np.where(s.astype(bool), s.str.join(' '), 'Other')
    print (df)
    
       No              Description       Keyword
    0   1        Paying Google ads        Google
    1   2  Purchasing Facebook Ads  Facebook Ads
    2   3  Purchasing LinkedIn Ads           Ads
    3   4         AirBnB repayment         Other
    

    您的解决方案:

    s = df['Description'].apply(lambda x: [i for i in set(df1['entity']) if i in x])
    df['Keyword'] = np.where(s.astype(bool), s.str.join(' '), 'Other')
    print (df)
       No              Description       Keyword
    0   1        Paying Google ads        Google
    1   2  Purchasing Facebook Ads  Facebook Ads
    2   3  Purchasing LinkedIn Ads           Ads
    3   4         AirBnB repayment         Other
    

    替代方案:

    out = []
    for x in df['Description']:
        L = [i for i in set(df1['entity']) if i in x]
        if bool(L):
            out.append(' '.join(L))
        else:
            out.append('Other')
    df['Keyword'] = out
    print (df)
       No              Description       Keyword
    0   1        Paying Google ads        Google
    1   2  Purchasing Facebook Ads  Facebook Ads
    2   3  Purchasing LinkedIn Ads           Ads
    3   4         AirBnB repayment         Other
    

    【讨论】:

      【解决方案2】:

      使用findall

      df.Description.str.findall(('|'.join(s.tolist()))).str[0]
      0      Google
      1    Facebook
      2         Ads
      3         NaN
      Name: Description, dtype: object
      df['Keyword']=df.Description.str.findall(('|'.join(s.tolist()))).str[0]
      

      数据输入

      s
      0      Google
      1    Facebook
      2         Ads
      Name: s, dtype: object
      

      【讨论】:

      • s是什么,你能详细说明一下吗?
      • @NabihBawazir 你的文本文件 entity.txt
      • 对不起,如果我们在一个描述中有多个关键字,它就不起作用
      【解决方案3】:

      使用str.extract()

      df['Keyword']=df.Description.str.extract(r'({})'.format('|'.join(df1[1],)))
      print(df)
      
        No              Description    Keyword
      0   1        Paying Google ads     Google
      1   2  Purchasing Facebook Ads   Facebook
      2   3  Purchasing LinkedIn Ads        Ads
      3   4         AirBnB repayment        NaN
      

      【讨论】:

      • 一个描述中有多个关键字怎么样?
      • @NabihBawazir 没明白,抱歉,您的意思是要为每一行映射多个关键字?如果是这样怎么办?请问可以加样吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多