Python 如何删除gensim中的stopwords?

Python 如何删除gensim中的stopwords?,python,gensim,Python,Gensim,我在数据帧的“message”列上尝试了此操作,但出现错误: df_clean['message'] = df_clean['message'].apply(lambda x: gensim.parsing.preprocessing.remove_stopwords(x)) 显然,df_clean[“message”]列包含一个单词列表,而不是一个字符串,因此出现了这样的错误:需要一个类似object的字节,list found 要解决此问题,需要使用如下方法将其再次转换为字符串: Type

我在数据帧的“message”列上尝试了此操作,但出现错误:

df_clean['message'] = df_clean['message'].apply(lambda x: gensim.parsing.preprocessing.remove_stopwords(x))

显然,
df_clean[“message”]
列包含一个单词列表,而不是一个字符串,因此出现了这样的错误:
需要一个类似object的字节,list found

要解决此问题,需要使用如下方法将其再次转换为字符串:

TypeError: decoding to str: need a bytes-like object, list found

请注意,
df_clean[“message”]
在应用上一个代码后将包含字符串对象。

这不是
gensim
问题,错误是由
pandas
引起的:列
message
中有一个值的类型是
list
,而不是
string
。下面是一个最小的
pandas
示例:

df_clean['message'] = df_clean['message'].apply(lambda x: gensim.parsing.preprocessing.remove_stopwords(" ".join(x)))

错误是,remove_stopwords需要string类型对象,并且您正在传递一个列表,因此在删除stop words之前,请检查列中的所有值是否为string类型

import pandas as pd
from gensim.parsing.preprocessing import remove_stopwords
df = pd.DataFrame([['one', 'two'], ['three', ['four']]], columns=['A', 'B'])
df.A.apply(remove_stopwords) # works fine

df.B.apply(remove_stopwords)

TypeError: decoding to str: need a bytes-like object, list found