Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pandas read_hdf具有大量条件_Python_Pandas_Dataframe_Pytables - Fatal编程技术网

Python pandas read_hdf具有大量条件

Python pandas read_hdf具有大量条件,python,pandas,dataframe,pytables,Python,Pandas,Dataframe,Pytables,我试图用pandas库读取64 GB的HDF文件(用blosc压缩)。数据帧包含3列和11410996915行。我尝试使用pandas.read_hdf方法和where参数按索引选择具体行。问题是,有时我需要获得数千行,因此在我的where参数中,我放置了如下内容: simMat = pd.read_hdf('global.h5','matrix', columns=['similarity'], where='index in {}'.format(listIdx)) 其中listIdx是一

我试图用pandas库读取64 GB的HDF文件(用blosc压缩)。数据帧包含3列和11410996915行。我尝试使用pandas.read_hdf方法和where参数按索引选择具体行。问题是,有时我需要获得数千行,因此在我的where参数中,我放置了如下内容:

simMat = pd.read_hdf('global.h5','matrix', columns=['similarity'], where='index in {}'.format(listIdx))
其中listIdx是一个整数列表,表示我想要返回的索引。当这个列表包含超过31个元素时,我得到一个内存错误。我开始查看pandas库的代码,发现在pytables.py文件中,在BinOp类中有一个名为_max_selectors的变量,该变量赋值为31。此变量在以下代码段中使用:

# if too many values to create the expression, use a filter instead
if self.op in ['==', '!='] and len(values) > self._max_selectors:

    filter_op = self.generate_filter_op()
    self.filter = (
       self.lhs,
       filter_op,
       pd.Index([v.value for v in values]))
    return self

使用筛选器会导致库尝试加载整个数据帧,这会引发MemoryError。我还尝试使用值为10的chunksize参数,但它也不起作用。您知道更好的方法来查询具有如此大索引集的HDF文件吗?

您可以使用以下技术:

pd.read_hdf(filename, 'key', where='index = vals')
其中
vals
pd.Series
Python列表
变量

您还可以使用属于其他DF的列进行筛选:

In [201]: another_df = pd.DataFrame(np.random.randint(0, 100, size=(100, 3)), columns=list('abc'))

In [202]: pd.read_hdf(fn, 'test', where='a = another_df["a"]').shape
Out[202]: (573, 3)
或其他DF的索引:

In [203]: pd.read_hdf(fn, 'test', where='index = another_df.index').shape
Out[203]: (100, 3)
演示:

设置

试验

与Python列表相同:

idx_list = vals.tolist()

In [197]: pd.read_hdf(fn, 'test', where='index = idx_list').shape
Out[197]: (98, 3)
vals = pd.Series(np.random.randint(0, 100, 500))

In [196]: pd.read_hdf(fn, 'test', where='index = vals').shape
Out[196]: (98, 3)
idx_list = vals.tolist()

In [197]: pd.read_hdf(fn, 'test', where='index = idx_list').shape
Out[197]: (98, 3)