【发布时间】:2019-11-07 04:29:35
【问题描述】:
我目前有一个包含一些单词或字符的列的数据框,我试图通过相应单元格中的搜索关键字对每一行进行分类。
例子
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
【问题讨论】:
-
不,那只适用于少量关键字,我正在为大量关键字寻找更简单的方法
标签: python python-3.x pandas numpy