将单词分词为pandas数据框中的新列 [英] Tokenizing words into a new column in a pandas dataframe

查看:134
本文介绍了将单词分词为pandas数据框中的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试浏览在熊猫数据框中收集的注释列表,并将这些单词标记化,然后将这些单词放在数据框中的新列中,但是我在运行时遇到了错误,是

I am trying to go through a list of comments collected on a pandas dataframe and tokenize those words and put those words in a new column in the dataframe but I have having an error running through this, is

错误表明AttributeError:'unicode'对象没有属性'apwords'

The error is stating that AttributeError: 'unicode' object has no attribute 'apwords'

还有其他方法可以做到吗?谢谢

Is there any other way to do this? Thanks

def apwords(words):
    filtered_sentence = []
    words = word_tokenize(words)
    for w in words:
        filtered_sentence.append(w)
    return filtered_sentence
addwords = lambda x: x.apwords()
df['words'] = df['complaint'].apply(addwords)
print df

推荐答案

您应用lambda函数的方法是正确的,这是您定义addwords的方法无效.

Your way to apply the lambda function is correct, it is the way you define addwords that doesn't work.

当您定义apwords时,您定义的是function而不是attribute,因此,当您要应用它时,请使用:

When you define apwords you define a function not an attribute therefore when you want to apply it, use:

addwords = lambda x: apwords(x)

不是:

addwords = lambda x: x.apwords()

如果要使用apwords作为属性,则需要定义一个从string继承的class,并在该类中将apwords定义为属性.

If you want to use apwords as an attribute, you would need to define a class that inheritates from string and define apwords as an attribute in this class.

使用function容易得多:

def apwords(words):
    filtered_sentence = []
    words = word_tokenize(words)
    for w in words:
        filtered_sentence.append(w)
    return filtered_sentence
addwords = lambda x: apwords(x)
df['words'] = df['complaint'].apply(addwords)

这篇关于将单词分词为pandas数据框中的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆