我正在编写一个函数,该函数将tweets的dataframe(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]
但是我一直返回前者而不是后者
任何见识将不胜感激。谢谢
答案 0 :(得分:1)
您的问题在这一行:
stop_words = list(stop_words_dict.values())
这将返回停用词列表的列表
替换为:
stop_words = stop_words_dict['stopwords']