Python 2.7 使用HDFStore的查询对选择进行迭代
我在HDFStore中有一个非常大的表,我希望使用查询选择其中的子集,然后逐块迭代子集。我希望查询在选择被分成块之前进行,这样所有的块大小都相同 文档似乎表明这是默认行为,但不太清楚。然而,在我看来,分块实际上发生在查询之前,如本例所示:Python 2.7 使用HDFStore的查询对选择进行迭代,python-2.7,pandas,Python 2.7,Pandas,我在HDFStore中有一个非常大的表,我希望使用查询选择其中的子集,然后逐块迭代子集。我希望查询在选择被分成块之前进行,这样所有的块大小都相同 文档似乎表明这是默认行为,但不太清楚。然而,在我看来,分块实际上发生在查询之前,如本例所示: In [1]: pd.__version__ Out[1]: '0.13.0-299-gc9013b8' In [2]: df = pd.DataFrame({'number': np.arange(1,11)}) In [3]: df Out[3]:
In [1]: pd.__version__
Out[1]: '0.13.0-299-gc9013b8'
In [2]: df = pd.DataFrame({'number': np.arange(1,11)})
In [3]: df
Out[3]:
number
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
[10 rows x 1 columns]
In [4]: with pd.get_store('test.h5') as store:
store.append('df', df, data_columns=['number'])
In [5]: evens = [2, 4, 6, 8, 10]
In [6]: with pd.get_store('test.h5') as store:
for chunk in store.select('df', 'number=evens', chunksize=5):
print len(chunk)
2
3
如果查询是在结果被分成块之前发生的,我希望只有一个大小为5的块,但是这个例子给出了两个长度为2和3的块
这是预期的行为吗?如果是的话,是否有一种有效的解决方法,可以在不将表读入内存的情况下提供相同大小的块?我想当我写这篇文章时,目的是使用查询结果的chunksize。我认为它在实施过程中发生了变化。chunksize确定应用查询的部分,然后对这些部分进行迭代。问题是,您事先不知道将得到多少行 然而,他们的方法是做到这一点。这是草图。使用select_作为实际执行查询的_坐标;这将返回坐标行号的Int64索引。然后将迭代器应用于根据这些行选择的位置 这是一个很好的配方,我认为文档中会包括:
In [15]: def chunks(l, n):
return [l[i:i+n] for i in xrange(0, len(l), n)]
....:
In [16]: with pd.get_store('test.h5') as store:
....: coordinates = store.select_as_coordinates('df','number=evens')
....: for c in chunks(coordinates, 2):
....: print store.select('df',where=c)
....:
number
1 2
3 4
[2 rows x 1 columns]
number
5 6
7 8
[2 rows x 1 columns]
number
9 10
[1 rows x 1 columns]
我想当我写这篇文章时,目的是使用查询结果的chunksize。我认为它在实施过程中发生了变化。chunksize确定应用查询的部分,然后对这些部分进行迭代。问题是,您事先不知道将得到多少行 然而,他们的方法是做到这一点。这是草图。使用select_作为实际执行查询的_坐标;这将返回坐标行号的Int64索引。然后将迭代器应用于根据这些行选择的位置 这是一个很好的配方,我认为文档中会包括:
In [15]: def chunks(l, n):
return [l[i:i+n] for i in xrange(0, len(l), n)]
....:
In [16]: with pd.get_store('test.h5') as store:
....: coordinates = store.select_as_coordinates('df','number=evens')
....: for c in chunks(coordinates, 2):
....: print store.select('df',where=c)
....:
number
1 2
3 4
[2 rows x 1 columns]
number
5 6
7 8
[2 rows x 1 columns]
number
9 10
[1 rows x 1 columns]