Answer a question

I want to remove the stop words from my column "tweets". How do I iterative over each row and each item?

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

Answers

Using List Comprehension

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

Returns:

0               [love, car]
1           [view, amazing]
2    [feel, great, morning]
3        [excited, concert]
4            [best, friend]
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐