【问题标题】:Check if key is in HDF5Store without path检查密钥是否在没有路径的 HDF5Store 中
【发布时间】:2014-02-13 18:48:37
【问题描述】:
使用 pandas/pytables,可以使用 store.keys() 轻松返回键列表。
>>> store.keys()
['/df_coord', '/metaFrame']
使用标准字典检查是否存在键 if 'df_coord' in store.keys():,除非包含 /,否则返回 false。是否有另一种简单的方法来评估键的存在而无需连接字符串?
【问题讨论】:
标签:
python
pandas
hdf5
pytables
【解决方案1】:
检查商店本身;他们.keys() 返回一个精确键的字符串字典。
In [1]: store = pd.HDFStore('test.h5',mode='w')
In [2]: store['foo'] = DataFrame(np.random.randn(10,2))
In [3]: store['bar'] = DataFrame(np.random.randn(10,2))
In [4]: store
Out[4]:
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/bar frame (shape->[10,2])
/foo frame (shape->[10,2])
In [5]: 'bar' in store
Out[5]: True
In [6]: 'foo' in store
Out[6]: True
In [7]: '/foo' in store
Out[7]: True
In [8]: 'bah' in store
Out[8]: False