如何在Python数据框中的每一行上使用split函数? [英] How to use the split function on every row in a dataframe in Python?

查看:231
本文介绍了如何在Python数据框中的每一行上使用split函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算单词在评论字符串中重复的次数

I want to count the number of times a word is being repeated in the review string

我正在读取csv文件,并使用将该文件存储在python数据框中在下面的行

I am reading the csv file and storing it in a python dataframe using the below line

reviews = pd.read_csv("amazon_baby.csv")

当我将其应用于单个评论时,以下行中的代码将起作用。

The code in the below lines work when I apply it to a single review.

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

当我对整个数据框应用相同的逻辑时使用下面的行。我收到错误消息

When I apply the same logic to the entire dataframe using the below line. I receive an error message

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'


推荐答案

您要拆分整个数据框的复查列(错误消息中提到的系列)。您想要做的是将一个函数应用于数据框的每一行,您可以通过调用应用

You're trying to split the entire review column of the data frame (which is the Series mentioned in the error message). What you want to do is apply a function to each row of the data frame, which you can do by calling apply on the data frame:

f = lambda x: len(x["review"].split("disappointed")) -1
reviews["disappointed"] = reviews.apply(f, axis=1)

这篇关于如何在Python数据框中的每一行上使用split函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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