小编典典

Python从熊猫数据框中删除停用词

python

我想从“ tweets”列中删除停用词。如何遍历每一行和每一项目?

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')]

test = pd.DataFrame(pos_tweets)
test.columns = ["tweet","class"]
test["tweet"] = test["tweet"].str.lower().str.split()

from nltk.corpus import stopwords
stop = stopwords.words('english')

阅读 215

收藏
2020-12-20

共1个答案

小编典典

使用列表理解

test['tweet'].apply(lambda x: [item for item in x if item not in stop])

返回值:

0               [love, car]
1           [view, amazing]
2    [feel, great, morning]
3        [excited, concert]
4            [best, friend]
2020-12-20