python-3.x - TypeError:熊猫数据框中的预期字符串或类似字节的对象
问题描述
我有包含 15000 条记录的文本数据集。我的数据框名称是 df1 并且文本列名称是干净的。我试图在文本列中找到现在、过去和将来时态单词的数量,并在下面的函数中使用。请注意,我从其中一个帖子中获取了此功能
from nltk import word_tokenize, pos_tag
def find_tense(sentence):
text = word_tokenize(sentence)
tagged = pos_tag(text)
global tense
tense = {}
tense["future"] = len([word for word in tagged if word[1] == "MD"])
tense["present"] = len([word for word in tagged if word[1] in ["VBP", "VBZ","VBG"]])
tense["past"] = len([word for word in tagged if word[1] in ["VBD", "VBN"]])
return(tense)
上述函数适用于一条记录,但是,当我尝试将其传递给数据框中的列时
find_tense(df1['clean'])
我得到以下错误
TypeError:预期的字符串或类似字节的对象
好心提醒。
解决方案
推荐阅读
- html - 响应式网格模板列,一列具有固定百分比宽度,第二列具有自动宽度
- kotlin - 读取一系列未定义大小的数字并打印其第一次出现的最大数字和位置
- node.js - 为什么我的环境会破坏?sha3的纱线安装错误
- javascript - 如何在Javascript中循环一个没有索引的对象数组作为字段?
- oracle - AWR 报告 Oracle OEM 中的时区更改
- python - 如何使用“列”方向将 Pandas 数据框中的索引隐藏到 JSON 函数 DataFrame.to_json()
- azure - 您可以向开发环境 AAD 令牌添加可选声明吗?
- angular - Ng-bootstrap 下拉菜单未在带有单击处理程序的表行上激活
- c# - 参数 ... 在方法中 ... 在服务中 ... 为空
- python - Django 抛出 ValueError:源代码字符串不能包含空字节