【问题标题】:Keywords search in text column of data frame using dictionary使用字典在数据框的文本列中搜索关键字
【发布时间】:2021-03-05 15:24:40
【问题描述】:

我是 python 新手,由于知识有限,我遇到了非常具体的要求,如果有人能提供帮助,我将不胜感激

我使用 excel 生成了一个字典,看起来像这样

dict = {'Fruit' : {'Comb Words' : ['yellow',
                                   'elongated',
                                   'cooking'],
                   'Mandatory Word' : ['banana',
                                       'banana',
                                       'banana']},
       'Animal' : {'Comb Words' : ['mammal',
                                   'white'
                                   'domestic'],
                  'Mandatory Word' : ['cat',
                                      'cat',
                                      'cat']}}

现在,我有一个包含文本列的数据框,我想将此字典中的关键字与该列匹配。例如:

            Text                     Mandatory      Comb            Final
A white domestic cat is playing        cat       domestic,white     Animal
yellow banana is not available        banana       yellow           Fruit

这本词典只是一个想法,我可以更改它,因为它是来自 excel 的输入。因此,任何其他可以导致上述输出的格式或方式都是这里唯一的目标。

【问题讨论】:

  • 您想要创建的只有这 3 列吗?
  • 理想情况下,但我更专注于解决方案。如果它需要超过 3 列,那也没关系

标签: python dataframe dictionary nlp keyword-search


【解决方案1】:

使用自定义函数:

import pandas as pd

Dict = {'Fruit' : {'Comb Words' : ['yellow',
                                   'elongated',
                                   'cooking'],
                   'Mandatory Word' : ['banana',
                                       'banana',
                                       'banana']},
       'Animal' : {'Comb Words' : ['mammal',
                                   'white',
                                   'domestic'],
                  'Mandatory Word' : ['cat',
                                      'cat',
                                      'cat']}}
                                      
df = pd.DataFrame({'Text':['A white domestic cat is playing',
                            'yellow banana is not available']})

def findMCF(sentence):
    for mand in sentence.split():
        for final in Dict:
            wordtypeDict = Dict[final]
            mandList = wordtypeDict['Mandatory Word']
            if mand in mandList:
                C = [wrd for wrd in sentence.split() if word in wordtypeDict['Comb Words']]
                return (mand,','.join(C),final)

df['Mandatory'],df['Comb'],df['Final'] = zip(*df['Text'].map(findMCF))

print(df)

输出:

                              Text Mandatory            Comb   Final
0  A white domestic cat is playing       cat  white,domestic  Animal
1   yellow banana is not available    banana          yellow   Fruit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2018-05-03
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多