【发布时间】:2020-06-15 09:53:22
【问题描述】:
如何在 Pandas Dataframe 列上应用词干提取
我正在使用这个函数进行词干提取,这对字符串非常有效
xx='kenichan dived times ball managed save 50 rest'
def make_to_base(x):
x_list = []
doc = nlp(x)
for token in doc:
lemma=str(token.lemma_)
if lemma=='-PRON-' or lemma=='be':
lemma=token.text
x_list.append(lemma)
print(" ".join(x_list))
make_to_base(xx)
但是当我在我的 pandas 数据框列上应用这个函数时,它既不工作也不给出任何错误
x = list(df['text']) #my df column
x = str(x)#converting into string otherwise it is giving error
make_to_base(x)
我尝试了不同的方法,但没有任何效果。像这样
df["texts"] = df.text.apply(lambda x: make_to_base(x))
make_to_base(df['text'])
我的数据集如下所示:
df['text'].head()
Out[17]:
0 Hope you are having a good week. Just checking in
1 K..give back my thanks.
2 Am also doing in cbe only. But have to pay.
3 complimentary 4 STAR Ibiza Holiday or £10,000 ...
4 okmail: Dear Dave this is your final notice to...
Name: text, dtype: object
【问题讨论】:
-
试试
df['texts'] = df['text'].apply(lambda x: make_to_base(str(x))) -
在 df['texts'] 中仅获取“无”值
-
然后发布一个示例数据框。
-
更新问题请查收
-
用
return " ".join(x_list)替换print(" ".join(x_list))