문제

Using pandas/pytables, a list of keys can be easily returned using store.keys().

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

Using the standard dictionary check to see if a key exists, if 'df_coord' in store.keys():, returns false unless the / is included. Is there another simple way to evaluate for the existence of a key without having to join strings?

도움이 되었습니까?

해결책

Check against the store itself; they .keys() returns a string dictionary of the exact 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top