【问题标题】:How to query a PyTables frame_table saved via a Pandas Dataframe?如何查询通过 Pandas Dataframe 保存的 PyTables frame_table?
【发布时间】:2016-11-22 15:30:15
【问题描述】:

我有以下熊猫数据框:

import pandas as pd
df = pd.read_table('fname.dat')

所以,我创建/打开一个现有的 HDFStore 文件:

store = pd.HDFStore('store.h5')

要索引列的子集,我只需使用

store.append('key_name', df, data_columns=['colA','colB','colZ'])

显然,HDFStore.append()默认以table 格式保存 pandas 数据帧。但是,看起来它实际上是一个“frame_table”对象:

store 

输出

 /key_name            frame_table  (typ->appendable,nrows->3254334,ncols->14,indexers->[index],dc->[colA, colB, colZ])

如何有效地索引这个对象?

通常情况下,查询将是

 result = [row for row in table.where('colA==22 & colB==45')]

但是对于frame_table 对象会这样做吗?

【问题讨论】:

    标签: python pandas hdf5 pytables h5py


    【解决方案1】:

    frame_table - 表示它是以table 格式保存的数据框。

    当使用data_columns=['colA','colB','colZ'] 参数时,您已经“索引”了['colA','colB','colZ'] 列。

    所以现在您可以按如下方式查询您的 HDFStore:

    store = pd.HDFStore('store.h5')
    varA = 100
    varZ = 'string_value'
    df = store.select('key_name', where='colA >= varA & colZ == varZ')
    

    您也可以使用pd.read_hdf(...) 代替store.select(...)

    PS 如果您提供样本和所需的数据集,答案可能会更加简洁......

    【讨论】:

      猜你喜欢
      • 2014-11-07
      • 2016-11-22
      • 2012-10-07
      • 1970-01-01
      • 2021-07-12
      • 2016-06-08
      • 2013-06-21
      相关资源
      最近更新 更多