Pandas Get All Values from Multiindex levels

懵懂的女人 提交于 2019-12-02 00:12:49

You can use get_level_values to get the index values at a specific level from a multi-index:

In [127]:
table.index.get_level_values('C')

Out[127]:
Index(['a', 'b', 'a', 'b', 'a', 'a', 'b', 'a', 'b'], dtype='object', name='C')

In [128]:    
table.index.get_level_values('B')

Out[128]:
Index(['x', 'x', 'y', 'y', 'z', 'x', 'y', 'z', 'z'], dtype='object', name='B')

In [129]:
table.index.get_level_values('A')

Out[129]:
Index(['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'], dtype='object', name='A')

get_level_values accepts an int param for the level or a label

Note that for the higher levels, the values are repeated to correspond with the index length at the lowest level, for display purposes you don't see this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!