2016-02-25 92 views
1

这可能是一个愚蠢的问题,但我还没有找到熊猫文档或其他地方的答案。 here之前询问过相同的问题。但唯一的答案就是看熊猫文档,正如我所说的那样,这些文档没有提供这个问题的答案。返回hdf文件中所有数据集的列表,熊猫

我希望能够用几个数据集建立一个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]) 

但是如果关闭HDF与store.close(),然后尝试它使用pd.read_hdf()以下错误返回到阅读:

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

是否有如何返回所有这些数据集的列表?

在此先感谢您的帮助!

回答

3

是的,有。

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])