【问题标题】:How can I remove English stop words using NLTK corpus from the Pandas dataframe text column?如何使用 NLTK 语料库从 Pandas 数据框文本列中删除英语停用词?
【发布时间】:2019-10-26 21:13:57
【问题描述】:

我正在寻找一种解决方案,以在 Pandas 数据框文本列上使用 NLTK 语料库删除英语停用词。可以用dataframe apply方法吗,如果可以,请分享一下?

stop_words = set(stopwords.words('english'))
data['text'] = data['text'].apply(lambda text:  " ".join(w) for w in text.lower().split() if w not in stop_words)

如果有人能回答,谢谢并感激不尽。

【问题讨论】:

    标签: python python-3.x pandas machine-learning nltk


    【解决方案1】:

    您可以标记您的文本列(或简单地拆分为单词列表),然后使用 mapapply 方法删除停用词。

    例如:

    data = pd.DataFrame({'text': ['a sentence can have stop words', 'stop words are common words like if, I, you, a, etc...']})
    data
                                                    text
    0                     a sentence can have stop words
    1  stop words are common words like if, I, you, a...
    
    from nltk.corpus import stopwords
    from nltk.tokenize import RegexpTokenizer
    
    tokenizer = RegexpTokenizer('\w+')
    stop_words = stopwords.words('english')
    
    def clean(x):
        doc = tokenizer.tokenize(x.lower())
        return [w for w in doc if w in stop_words]
    
    data.text.map(clean)
    0                    [sentence, stop, words]
    1    [stop, words, common, words, like, etc]
    Name: text, dtype: object
    

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 2017-11-28
      • 2020-05-12
      • 2015-08-03
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      相关资源
      最近更新 更多