python – Pandas:使用read_csv时如何获取读取行的状态?

前端之家收集整理的这篇文章主要介绍了python – Pandas:使用read_csv时如何获取读取行的状态?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用pandas和read_csv方法加载一个非常庞大的csv文件,如1000万条记录,我想知道是否有办法显示该加载的进度,如:

100,000 lines read
150,000 lines read

谢谢.

最佳答案
显示这样的进度:

Completed 1 %
Completed 2 % 
... 
Completed 99 % 
Completed 100 %

你可以试试这个:

import os,pandas
filename = "VeryLong.csv"
lines_number = sum(1 for line in open(filename))
lines_in_chunk = 500 # I don't know what size is better
counter = 0
completed = 0
reader = pandas.read_csv(filename,chunksize=lines_in_chunk)
for chunk in reader:
    # < ... reading the chunk somehow... >
    # showing progress:
    counter += lines_in_chunk
    new_completed = int(round(float(counter)/lines_number * 100))
    if (new_completed > completed): 
        completed = new_completed
        print "Completed",completed,"%"
原文链接:https://www.f2er.com/python/438718.html

猜你在找的Python相关文章