如何访问pandas HDStore(pyTables)中的索引

2024-09-27 00:23:13 发布

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

我有一个带多索引的大型hdf存储。我怎样才能掌握其中一个指数级别?我知道我可以这样访问colindex:

 store._handle.root.data.table.colindexes

但是还没有得到一个列索引的列表。在


Tags: store列表datatableroot指数级别handle
1条回答
网友
1楼 · 发布于 2024-09-27 00:23:13

来自docs的多索引示例

In [21]: index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
                                    ['one', 'two', 'three']],
                                    labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
                                    [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
                                    names=['foo', 'bar'])

In [22]: df_mi = DataFrame(np.random.randn(10, 3), index=index,columns=['A', 'B', 'C'])

In [23]: df_mi
Out[23]: 
                  A         B         C
foo bar                                
foo one    0.385666 -0.013980  1.787451
    two   -0.190728  0.574169 -0.115581
    three  0.823308  1.845204  0.603430
bar one   -0.863913 -1.544945 -0.598322
    two   -0.941484  1.080205 -0.086216
baz two   -0.510657  0.205781  1.747204
    three -1.322135 -1.131720 -0.838862
qux one    0.346890 -0.905762  0.348206
    two   -1.378711 -0.751701  2.229486
    three  0.120956 -0.535718 -0.344610

以表格式存储(请注意,在0.13中,您将传递format='table'

^{pr2}$

检索单个列。MI存储为列!在

In [25]: store = pd.HDFStore('test.h5')

In [26]: store.select_column('df_mi','foo')
Out[26]: 
0    foo
1    foo
2    foo
3    bar
4    bar
5    baz
6    baz
7    qux
8    qux
9    qux
dtype: object

In [30]: store.select_column('df_mi','bar')
Out[30]: 
0      one
1      two
2    three
3      one
4      two
5      two
6    three
7      one
8      two
9    three
dtype: object

In [31]: store.close()

相关问题 更多 >

    热门问题