【问题标题】:return a list of all datasets in a hdf file with pandas使用 pandas 返回 hdf 文件中所有数据集的列表
【发布时间】:2016-06-08 19:02:57
【问题描述】:

这可能是一个愚蠢的问题,但我还没有在 pandas 文档或其他地方找到答案。 here 之前也有人问过同样的问题。但唯一的答案是查看 pandas 文档,正如我所说,它没有为这个问题提供答案。

我希望能够构建一个包含多个数据集的 hdf 文件。关闭此 hdf 后,我希望能够列出其中包含的每个数据集。例如:

import pandas as pd
import numpy as np

store = pd.HDFStore('test.h5')
df1 = pd.DataFrame(np.random.randn(10,2), columns=list('AB')
df2 = pd.DataFrame(np.random.randn(10,2), columns=list('AB')
store['df1'] = df1
store['df2'] = df2
print(store)

返回:

<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df1           frame          (shape->[10,2])
/df2           frame          (shape->[10,2])

但是,如果您使用 store.close() 关闭 hdf,然后尝试使用 pd.read_hdf() 读取它,则会返回以下错误:

ValueError: key must be provided when HDF contains multiple datasets.

有没有办法返回所有这些数据集的列表?

提前感谢您的帮助!

【问题讨论】:

    标签: python pandas hdf


    【解决方案1】:

    是的,有。

    store = pd.HDFStore('test.h5')
    print(store)
    
    <class 'pandas.io.pytables.HDFStore'>
    File path: test.h5
    /df1           frame          (shape->[10,2])
    /df2           frame          (shape->[10,2])
    

    【讨论】:

    猜你喜欢
    • 2020-05-13
    • 2014-09-07
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    相关资源
    最近更新 更多