我正在关注下一个问题:Python remove stop words from pandas dataframe
但是对于自定义停用词列表而言,它对我不起作用,请查看以下代码:
pos_tweets = [('I love this car', 'positive'),
('This view is amazing', 'positive'),
('I feel great this morning', 'positive'),
('I am so excited about the concert', 'positive'),
('He is my best friend', 'positive')]
import pandas as pd
test = pd.DataFrame(pos_tweets)
test.columns = ["tweet","col2"]
test["tweet"] = test["tweet"].str.lower().str.split()
stop = ['love','car','amazing']
test['tweet'].apply(lambda x: [item for item in x if item not in stop)
print test
结果是:
tweet col2
0 [i, love, this, car] positive
1 [this, view, is, amazing] positive
2 [i, feel, great, this, morning] positive
3 [i, am, so, excited, about, the, concert] positive
4 [he, is, my, best, friend] positive
爱,汽车和惊人的词仍然存在,我还缺少什么?
谢谢!
最佳答案
您需要将输出分配回列tweet
:
test['tweet'] = test['tweet'].apply(lambda x: [item for item in x if item not in stop])
print (test)
tweet col2
0 [i, this] positive
1 [this, view, is] positive
2 [i, feel, great, this, morning] positive
3 [i, am, so, excited, about, the, concert] positive
4 [he, is, my, best, friend] positive