我目前有一个数据框,其中的一列包含一些单词或字符,我试图通过该对应单元格中的搜索关键字对每一行进行分类。
示例
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',(df['words'].str.contains('form',(df['words'].str.contains('scotia',(df['words'].str.contains('news',(df_prt_copy['words'].str.contains('questions.*\.pdf',.
.
.
.
]
choices = ['open house flyer','report','form','news',‘question',.
.
.
.
]
df['category']=np.select(conditions,choices,default='others')
这很好用,但是问题是我有很多关键字(大概超过120个左右),因此维护此关键字列表非常困难,有没有更好的方法呢? 顺便说一句,我正在使用python3
注意:我正在寻找一种更简单的方法来管理大量关键字,这不同于简单的找到关键字here
的方法。