简体   繁体   English

如何在 pandas read_csv 中再次从头读取文件?

[英]How to read the file from the beginning again in pandas read_csv?

How to read the file from the beginning again after reached to the end?到达结尾后如何重新从头读取文件?

I can use a loop to open and close the same file, but it's not good in performance.我可以使用循环来打开和关闭同一个文件,但性能不佳。

with pd.read_csv("myfile.csv", chunksize=10**6) as reader:
    for chunk in reader:
        for _, row in chunk.iterrows():
            do_something(row)

If you need to do something utilizing the chunksize parameter, looping or re-reading the file from scratch is your only option since the object returned when that option is used is an iterable, so once you reach the end of the iterable, it will be empty.如果您需要使用 chunksize 参数执行某些操作,则从头开始循环或重新读取文件是您唯一的选择,因为使用该选项时返回的 object 是可迭代的,因此一旦到达可迭代的末尾,它将是空的。

See Iterating through files chunk by chunk请参阅逐块迭代文件

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

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