使用" chunksize"打开带有pandas的选定行和/或"迭代器"

时间:2016-08-20 11:31:45

标签: python pandas csv

我有一个大的csv文件,我用pd.read_csv打开它,如下所示:

df = pd.read_csv(path//fileName.csv, sep = ' ', header = None)

由于文件非常大,我希望能够在行中打开它

from 0 to 511
from 512 to 1023
from 1024 to 1535
...
from 512*n to 512*(n+1) - 1

其中n = 1,2,3 ......

如果我将chunksize = 512添加到read_csv

的参数中
df = pd.read_csv(path//fileName.csv, sep = ' ', header = None, chunksize = 512)

然后输入

df.get_chunk(5)

我可以打开从0到5的行,或者我可以使用for循环将文件分成512行的部分

data = []
for chunks in df:
    data = data + [chunk]

但是这很无用,因为文件必须完全打开并且需要时间。如何只读取512 * n到512 *(n + 1)的行。

环顾四周,我经常看到" chunksize"与" iterator"一起使用如下:

 df = pd.read_csv(path//fileName.csv, sep = ' ', header = None, iterator = True, chunksize = 512)

但经过多次尝试后,我仍然不明白哪些好处为我提供了这个布尔变量。你可以解释一下吗?

1 个答案:

答案 0 :(得分:14)

  

如何只读取512 * n到512 *(n + 1)的行?

df = pd.read_csv(fn, header=None, skiprows=512*n, nrows=512)

你可以这样做(而且它非常有用):

for chunk in pd.read_csv(f, sep = ' ', header = None, chunksize = 512):
    # process your chunk here

演示:

In [61]: fn = 'd:/temp/a.csv'

In [62]: pd.DataFrame(np.random.randn(30, 3), columns=list('abc')).to_csv(fn, index=False)

In [63]: for chunk in pd.read_csv(fn, chunksize=10):
   ....:     print(chunk)
   ....:
          a         b         c
0  2.229657 -1.040086  1.295774
1  0.358098 -1.080557 -0.396338
2  0.731741 -0.690453  0.126648
3 -0.009388 -1.549381  0.913128
4 -0.256654 -0.073549 -0.171606
5  0.849934  0.305337  2.360101
6 -1.472184  0.641512 -1.301492
7 -2.302152  0.417787  0.485958
8  0.492314  0.603309  0.890524
9 -0.730400  0.835873  1.313114
          a         b         c
0  1.393865 -1.115267  1.194747
1  3.038719 -0.343875 -1.410834
2 -1.510598  0.664154 -0.996762
3 -0.528211  1.269363  0.506728
4  0.043785 -0.786499 -1.073502
5  1.096647 -1.127002  0.918172
6 -0.792251 -0.652996 -1.000921
7  1.582166 -0.819374  0.247077
8 -1.022418 -0.577469  0.097406
9 -0.274233 -0.244890 -0.352108
          a         b         c
0 -0.317418  0.774854 -0.203939
1  0.205443  0.820302 -2.637387
2  0.332696 -0.655431 -0.089120
3 -0.884916  0.274854  1.074991
4  0.412295 -1.561943 -0.850376
5 -1.933529 -1.346236 -1.789500
6  1.652446 -0.800644 -0.126594
7  0.520916 -0.825257 -0.475727
8 -2.261692  2.827894 -0.439698
9 -0.424714  1.862145  1.103926
  

在哪种情况下"迭代器"可能有用吗?

使用chunksize时 - 所有块的长度都相同。使用iterator参数,您可以定义每次迭代中要读取的数据量(get_chunk(nrows)):

In [66]: reader = pd.read_csv(fn, iterator=True)

让我们先阅读3行

In [67]: reader.get_chunk(3)
Out[67]:
          a         b         c
0  2.229657 -1.040086  1.295774
1  0.358098 -1.080557 -0.396338
2  0.731741 -0.690453  0.126648

现在我们将阅读接下来的5行:

In [68]: reader.get_chunk(5)
Out[68]:
          a         b         c
0 -0.009388 -1.549381  0.913128
1 -0.256654 -0.073549 -0.171606
2  0.849934  0.305337  2.360101
3 -1.472184  0.641512 -1.301492
4 -2.302152  0.417787  0.485958

接下来的7行:

In [69]: reader.get_chunk(7)
Out[69]:
          a         b         c
0  0.492314  0.603309  0.890524
1 -0.730400  0.835873  1.313114
2  1.393865 -1.115267  1.194747
3  3.038719 -0.343875 -1.410834
4 -1.510598  0.664154 -0.996762
5 -0.528211  1.269363  0.506728
6  0.043785 -0.786499 -1.073502