简体   繁体   中英

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.

See Iterating through files chunk by chunk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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