【问题标题】:How to access nested tables in hdf5 with pandas如何使用 pandas 访问 hdf5 中的嵌套表
【发布时间】:2019-09-06 22:57:33
【问题描述】:

我想使用 pandas 从 HDF5 文件中检索表。

根据我找到的几个参考资料,我尝试使用以下方法打开文件:

df = pd.read_hdf('data/test.h5', g_name), 

其中 g_name 是我要检索的对象的路径,即表 TAB1,例如 MAIN/Basic/Tables/TAB1。

g_name 检索如下:

def get_all(name):
    if 'TAB1' in name:
        return name

with h5py.File('data/test.h5') as f:
    g_name = f.visit(get_all)
    print(g_name)
    group = f[g_name]
    print(type(group))

我也尝试过检索对象本身,如上面的代码 sn-p 所示,但对象类型是 我如何将其转换为可以在熊猫中读取为数据框的内容?

对于第一种情况,我收到以下错误: "如果对象不存在,则无法创建存储"

如果路径与搜索期间检索到的路径相同,我不明白为什么找不到对象。

【问题讨论】:

    标签: pandas hdf5


    【解决方案1】:

    我找到了以下解决方案:

    hf = h5py.File('data/test.h5')
    data = hf.get('MAIN/Basic/Tables/TAB1')
    result = data[()]
    
    # This last step just converts the table into a pandas df
    df = pd.DataFrame(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多