我有一个带有多索引的大型HDFStore。如何获得其中一个索引级别?我看到我可以这样访问colindex:
store._handle.root.data.table.colindexes
但尚未获得其中一个列索引的列表。
答案 0 :(得分:5)
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'
)
In [24]: df_mi.to_hdf('test.h5','df_mi',table=True,mode='w')
检索单个列。 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()