How can I lemmatize strings in pandas dataframes?(我如何才能将 pandas 数据帧中的字符串列举出来?)
本文介绍了我如何才能将 pandas 数据帧中的字符串列举出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Python Pandas DataFrame,其中我需要对其中两列中的单词进行词汇化。我正在使用Spacy进行此操作。
import spacy
nlp = spacy.load("en")
我正在尝试使用基于此示例的词汇化(它工作得非常好):
doc3 = nlp(u"this is spacy lemmatize testing. programming books are more better than others")
for token in doc3:
print (token, token.lemma, token.lemma_)
我已经重写了这段代码,以循环访问我的数据帧中一列的每一行:
for row in example['col1']:
for token in row:
print(token.lemma_)
这很管用,但是我想不出如何用词汇化的单词替换col1中的单词。
我试过了,它不会返回错误,也不会替换任何单词。知道哪里出了问题吗?
for row in example['col1']:
for token in row:
token = token.lemma_
推荐答案
在代码的最后一个for循环中,您重复地将其属性token赋给变量token.lemma_,然后一次又一次地执行此操作(在每次迭代时覆盖该属性,而不跟踪以前的值)。
相反,假设您的数据帧包含字符串,如
example = pd.DataFrame({"col1":["this is spacy lemmatization testing.", "some programming books are better than others", "sounds like a quote from the Smiths"]})
apply和列表理解可以使用:
example["col1"].apply(lambda row: " ".join([w.lemma_ for w in nlp(row)]))
这篇关于我如何才能将 pandas 数据帧中的字符串列举出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:我如何才能将 pandas 数据帧中的字符串列举出来?
基础教程推荐
猜你喜欢
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
