我认为您可以使用该nrows
参数。从文档:
nrows : int, default None
Number of rows of file to read. Useful for reading pieces of large files
这似乎有效。使用标准大型测试文件之一(988504479字节,5344499行):
In [1]: import pandas as pd
In [2]: time z = pd.read_csv("P00000001-ALL.csv", nrows=20)
cpu times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
In [3]: len(z)
Out[3]: 20
In [4]: time z = pd.read_csv("P00000001-ALL.csv")
cpu times: user 27.63 s, sys: 1.92 s, total: 29.55 s
Wall time: 30.23 s