如何解决从熊猫数据框中删除自定义停用词不起作用
我正在尝试删除自定义停用词列表,但它不起作用。
desc = pd.DataFrame(description,columns =['description'])
print(desc)
结果如下
description
188693 The Kentucky Cannabis Company and Bluegrass He...
181535 Ohio County Sheriff
11443 According to new reports from federal authorit...
213919 KANSAS CITY,Mo. (AP)The Chiefs will be withou...
171509 The crew of Insight,WCNY's weekly public affa...
... ...
2732 The Arkansas Supreme Court on Thursday cleared...
183367 Larry Pegram,co-owner of Pure Ohio Wellness,...
134291 Joe Biden will spend the next five months pres...
239270 Find out where your Texas representatives stan...
246070 SAN TAN VALLEY — Two men have been charged wit...
[9875 rows x 1 columns]
我在这里找到了以下代码,但它似乎不起作用
remove_words = ["marijuana","cannabis","hemp","thc","cbd"]
pat = '|'.join([r'\b{}\b'.format(w) for w in remove_words])
desc.assign(new_desc=desc.replace(dict(string={pat: ''}),regex=True))
产生以下结果
description new_desc
188693 The Kentucky Cannabis Company and Bluegrass He... The Kentucky Cannabis Company and Bluegrass He...
181535 Ohio County Sheriff Ohio County Sheriff
11443 According to new reports from federal authorit... According to new reports from federal authorit...
213919 KANSAS CITY,Mo. (AP)The Chiefs will be withou... KANSAS CITY,WCNY's weekly public affa... The crew of Insight,WCNY's weekly public affa...
... ... ...
2732 The Arkansas Supreme Court on Thursday cleared... The Arkansas Supreme Court on Thursday cleared...
183367 Larry Pegram,... Larry Pegram,...
134291 Joe Biden will spend the next five months pres... Joe Biden will spend the next five months pres...
239270 Find out where your Texas representatives stan... Find out where your Texas representatives stan...
246070 SAN TAN VALLEY — Two men have been charged wit... SAN TAN VALLEY — Two men have been charged wit...
9875 rows × 2 columns
如您所见,停用词并未删除。您能提供的任何帮助将不胜感激。
解决方法
处理案例,简化模式,
remove_words = ["marijuana","cannabis","hemp","thc","cbd"]
pat = '|'.join(remove_words)
desc['new_desc'] = desc.description.str.lower().replace(pat,'',regex=True)
description new_desc
0 The Kentucky Cannabis Company and Bluegrass He... the kentucky company and bluegrass he...
1 Ohio County Sheriff ohio county sheriff
2 According to new reports from federal authorit... according to new reports from federal authorit...
3 KANSAS CITY,Mo. (AP)The Chiefs will be mariju... kansas city,mo. (ap)the chiefs will be witho...
4 The crew of Insight,WCNY's weekly public affa... the crew of insight,wcny's weekly public affa...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。