python - 在数据框单元格中搜索关键字

标签 python python-3.x pandas numpy

我目前有一个数据框,其中有一列包含一些单词或字符,我试图通过相应单元格中的搜索关键字对每一行进行分类。

示例

  words             |   category
-----------------------------------
im a test email     |  email
here is my handout  |  handout

这就是我所拥有的

conditions = [
        (df['words'].str.contains('flyer',False,regex=True)),
        (df['words'].str.contains('report',False,regex=True)),
        (df['words'].str.contains('form',False,regex=True)), 
        (df['words'].str.contains('scotia',False,regex=True)),  
        (df['words'].str.contains('news',False,regex=True)), 
         (df_prt_copy['words'].str.contains('questions.*\.pdf',False,regex=True)),
         .
         .
         .
         .
    ]
    choices = ['open house flyer', 
               'report', 
               'form', 
               'report',
               'news', 
               ‘question',
                  .
                  .
                  .
                  .
              ]
     df['category']=np.select(conditions, choices, default='others')

这工作正常,但问题是我有很多关键字(可能超过 120 个左右),所以维护这个关键字列表非常困难,有没有更好的方法来做到这一点? 顺便说一句,我正在使用 python3

注意:我正在寻找一种更简单的方法来管理大量关键字,这与简单地查找关键字的方法不同 here

最佳答案

如果一行中有多个关键字,您可以连接所有关键字并使用 str.findall,然后map 到 cond 与 Choices 的字典:

df = pd.DataFrame({"words":["im a test email",
                            "here is my handout",
                            "This is a flyer"]})

choices = {"flyer":"open house flyer",
           "email":"email from someone",
           "handout":"some handout"}

df["category"] = df["words"].str.findall("|".join(choices.keys())).str.join(",").map(choices)

print (df)

#
                words            category
0     im a test email  email from someone
1  here is my handout        some handout
2     This is a flyer    open house flyer

关于python - 在数据框单元格中搜索关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58742006/

相关文章:

python - 在 python 中使用 IfcOpenShell 提取元素数量

python - 如何在 Python 指针中有效地迭代数组?

python - 如何使用 Python/Pandas 从日期字段按月、日分组

python - gtk3 和 pygobject 的自动完成

python - 如何将整个 python 空闲命令历史记录打印到文本文件

python - 创建新列时日期转换不起作用

r - 是否有与 R 的 biglm 等效的 Python?

Python Selenium 发送键给出大小警告

python - 如何在seaborn图表中循环执行子图

python - PIL 的舍入误差