【发布时间】:2016-07-06 14:58:51
【问题描述】:
我想计算一个单词在评论字符串中重复的次数
我正在读取 csv 文件并使用以下行将其存储在 python 数据框中
reviews = pd.read_csv("amazon_baby.csv")
当我将以下行中的代码应用于单个评论时,它可以工作。
print reviews["review"][1]
a = reviews["review"][1].split("disappointed")
print a
b = len(a)
print b
以上行的输出是
it came early and was not disappointed. i love planet wise bags and now my wipe holder. it keps my osocozy wipes moist and does not leak. highly recommend it.
['it came early and was not ', '. i love planet wise bags and now my wipe holder. it keps my osocozy wipes moist and does not leak. highly recommend it.']
2
当我使用下面的行将相同的逻辑应用于整个数据框时。我收到一条错误消息
reviews['disappointed'] = len(reviews["review"].split("disappointed"))-1
错误信息:
Traceback (most recent call last):
File "C:/Users/gouta/PycharmProjects/MLCourse1/Classifier.py", line 12, in <module>
reviews['disappointed'] = len(reviews["review"].split("disappointed"))-1
File "C:\Users\gouta\Anaconda2\lib\site-packages\pandas\core\generic.py", line 2360, in __getattr__
(type(self).__name__, name))
AttributeError: 'Series' object has no attribute 'split'
【问题讨论】: