首页 > 解决方案 > 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:预期的字符串或类似字节的对象

在此处输入图像描述

好心提醒。

标签: python-3.xpandasdataframe

解决方案


推荐阅读