为什么我使用pandas读取csv文件的对象是TextFileReader对象 [英] Why the object, which I read a csv file using pandas from, is TextFileReader object

查看:698
本文介绍了为什么我使用pandas读取csv文件的对象是TextFileReader对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用熊猫读取了一个csv文件:

I read a csv file using pandas:

data_raw = pd.read_csv(filename, chunksize=chunksize)
print(data_raw['id'])

然后,它报告TypeError:

Then, it reports TypeError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'TextFileReader' object has no attribute '__getitem__'

我该怎么做才能解决问题?以及如何将data_raw更改为dataFrame对象? 我使用python2.7和pandas v0.19.1

What can I do to resolve the problem? And how can I change the data_raw into a dataFrame object? I use the python2.7 and pandas v0.19.1

推荐答案

解决此问题的一种方法是在pd.read_csv()函数中设置nrows参数,然后选择要加载到数据框中的数据子集.当然,缺点是您将无法查看和使用完整的数据集.代码示例:

Oone way around this problem is to set nrows parameter in pd.read_csv() function and that way you select subset of data you want to load into the dataframe. Of course, drawback is that you wont be able to see and work with full dataset. Code example:

data = pd.read_csv(filename, nrows=100000)

这篇关于为什么我使用pandas读取csv文件的对象是TextFileReader对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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