如何在pandas read_csv中从头开始重新读取文件



到达末尾后如何从头开始重新读取文件?

我可以使用循环来打开和关闭同一个文件,但它的性能不好。

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

如果你需要利用chunksize参数来做一些事情,循环或从头开始重新读取文件是你唯一的选择,因为使用该选项时返回的对象是可迭代的,所以一旦你到达可迭代的末尾,它就会是空的。

请参阅逐块遍历文件

最新更新