检查HDF5Store中的键是否有路径

2024-10-03 00:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

使用pandas/pytables,可以使用store.keys()轻松返回键列表。在

>>> store.keys()
['/df_coord', '/metaFrame']

使用标准字典检查是否存在键if 'df_coord' in store.keys():,除非包含/,否则返回false。有没有另一种简单的方法来评估键的存在,而不必连接字符串?在


Tags: 方法store字符串infalsepandasdf列表
1条回答
网友
1楼 · 发布于 2024-10-03 00:28:03

检查存储本身;它们.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

相关问题 更多 >