Skip to content Skip to sidebar Skip to footer

How To Retrieve Pandas Df Multiindex From Hdfstore?

If DataFrame with simple index is the case, one may retrieve index from HDFStore as follows: df = pd.DataFrame(np.random.randn(2, 3), index=list('yz'), columns=list('abc')) df >

Solution 1:

One may use select with columns=['index'] parameter specified:

df = pd.DataFrame(np.random.randn(2, 3),
                  index=pd.MultiIndex.from_tuples([(0,'y'), (1, 'z')], names=['lvl0', 'lvl1']),
                  columns=list('abc'))
df

>>>                 a           b           c
>>> lvl0    lvl1            
>>> 0       y    -0.8711250.0017730.618647>>> 1       z     1.0015471.132322    -0.215681
with pd.HDFStore('test.h5') as store:
    store.put('df', df, format='t')
    store.select('df', columns=['index'])

>>> lvl0    lvl1
>>> 0       y
>>> 1       z

It works but seems not being documented.

Post a Comment for "How To Retrieve Pandas Df Multiindex From Hdfstore?"