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

Viewed 22

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)
1 Answers

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

Related