是否可以使用 Pytables(或 Pandas)来检测 hdf 文件的表是否包含特定列?要加载我使用的 hdf 文件:
from pandas.io.pytables import HDFStore
# this doesn't read the full file which is good
hdf_store = HDFStore('data.h5', mode='r')
# returns a "Group" object, not sure if this could be used...
hdf_store.get_node('tablename')
我也可以直接使用 Pytables 而不是 Pandas。目的不是加载 hdf 文件的所有数据,因为这些文件可能很大,我只想确定某个列是否存在。
请您参考如下方法:
我可能找到了解决方案,但不确定 (1) 它为何有效以及 (2) 这是否是一个可靠的解决方案。
import tables
h5 = tables.openFile('data.h5', mode='r')
df_node = h5.root.__getattr__('tablename')
# Not sure why `axis0` contains the column data, but it seems consistent
# with the tested h5 files.
columns = df_node.axis0[:]
columns 包含一个包含所有列名的 numpy 数组。
