【发布时间】:2020-08-29 10:03:32
【问题描述】:
我正在使用 Gensim LDA 进行主题建模。我正在使用 pandas DataFrame 进行处理。但我收到一个错误
TypeError: 解码为 str: 需要一个类似字节的对象,已找到系列
我只需要使用 Pandas 处理数据,输入数据就像(一行)
PMID Text
12755608 The DNA complexation and condensation properties
12755609 Three proteins namely protective antigen PA edition
12755610 Lecithin retinol acyltransferase LRAT catalyze
我的代码是
data = pd.read_csv("h1.csv", delimiter = "\t")
data = data.dropna(axis=0, subset=['Text'])
data['Index'] = data.index
data["Text"] = data['Text'].str.replace('[^\w\s]','')
data.head()
def lemmatize_stemming(text):
return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))
def preprocess(text):
result = []
for token in gensim.utils.simple_preprocess(text):
if token not in gensim.parsing.preprocessing.STOPWORDS and len(token):
result.append(lemmatize_stemming(token))
return result
input_data = data.Text.str.strip().str.split('[\W_]+')
print('\n\n tokenized and lemmatized document: ')
print(preprocess(input_data))
【问题讨论】:
-
根据错误消息,我猜您的函数需要一个字符串,例如“DNA 络合和凝聚特性”。相反,您正在为函数提供 pandas.Series。如果没有虚拟数据,就很难准确地确定错误出现的位置以及如何解决它
-
@KenHBS 我已经更新了数据,其余行类似,是的,我需要传递一个字符串,有什么建议吗?
标签: python pandas dataframe gensim lda