是否可以使用Pytable(或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')我也可以直接使用Pytable而不是Pandas。目的不是加载hdf文件的所有数据,因为这些文件可能很大,我只想确定某个列是否存在。
发布于 2016-07-12 08:07:08
我可能已经找到了一个解决方案,但我不确定(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数组。
发布于 2017-08-13 14:32:05
对于Pandas0.20.3和PyTables 3.3.0 (使用Pandas创建的HDF文件),接受的解决方案对我不起作用。然而,这样做是可行的:
pd.HDFStore('data.hd5', mode='r').get_node('/path/to/pandas/df').table.colnameshttps://stackoverflow.com/questions/38321684
复制相似问题