简体   繁体   English

从 TextFileReader 对象转换为 Pandas DataFrame

[英]Converting from TextFileReader object to pandas DataFrame

I have this code:我有这个代码:

f = pd.read_csv(data,delimiter=",",chunksize=1000000)
print(f)
f.head()

Which uses pandas to read the csv file with name from the variable data.它使用熊猫从变量数据中读取带有名称的 csv 文件。

I cannot use the head function as it is a TextFileReader object (the output of print(f) is "pandas.io.parsers.TextFileReader object at 0x78a9180da6d8”)我不能使用 head 函数,因为它是一个 TextFileReader 对象(print(f) 的输出是“pandas.io.parsers.TextFileReader 对象在 0x78a9180da6d8”)

The error I receive is: AttributeError: 'TextFileReader' object has no attribute 'head'我收到的错误是:AttributeError: 'TextFileReader' object has no attribute 'head'

How do I convert from this object to a pandas dataframe?如何从此对象转换为熊猫数据框?

This is to do with the Chunk argument.这与 Chunk 论点有关。 The TextFileReader object contains the chunks and so you must use: TextFileReader 对象包含块,因此您必须使用:

for chunk in f:
    print(chunk) #or whatever other command

As suggested by User Jon Clements:正如用户 Jon Clements 所建议的:

https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#iterating-through-files-chunk-by-chunk https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#iterating-through-files-chunk-by-chunk

Has full documentation on this area.有关于这方面的完整文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM