簡體   English   中英

如何使用自定義停用詞詞典從數據框列中刪除英語停用詞

[英]How do I remove english stop words from a dataframe column using a custom dictionary of stop words

我正在編寫一個函數,它將推文的數據幀(df)作為輸入。 我需要標記推文並刪除停用詞並將此輸出添加到新列。 除了 numpy 和 pandas 之外,我不能導入任何東西。

停用詞在字典中如下:

stop_words_dict = {
'stopwords':[
    'where', 'done', 'if', 'before', 'll', 'very', 'keep', 'something', 'nothing', 'thereupon', 
    'may', 'why', '’s', 'therefore', 'you', 'with', 'towards', 'make', 'really', 'few', 'former', 
    'during', 'mine', 'do', 'would', 'of', 'off', 'six', 'yourself', 'becoming', 'through', 
    'seeming', 'hence', 'us', 'anywhere....}

這就是我試圖做的:刪除停用詞的函數

def stop_words_remover(df):
    stop_words = list(stop_words_dict.values())
    df["Without Stop Words"] = df["Tweets"].str.lower().str.split()
    df["Without Stop Words"] = df["Without Stop Words"].apply(lambda x: [word for word in x if word not in stop_words])
    return df

所以如果這是我的輸入:

 [@bongadlulane, please, send, an, email, to,]

這是預期的輸出:

[@bongadlulane, send, email, mediadesk@eskom.c]

但我一直返回前者而不是后者

任何見解將不勝感激。 謝謝

你的問題在這一行:

stop_words = list(stop_words_dict.values())

這將返回停用詞列表的列表

替換為:

stop_words = stop_words_dict['stopwords']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM