How to get sub_level index value for Pandas dataframe?

I have a multi-level df data file:

 >>> df sales cash STK_ID RPT_Date 000568 20120630 51.926 42.845 20120930 80.093 57.488 000596 20120630 22.278 18.247 20120930 32.585 26.177 000799 20120630 9.291 6.513 20120930 14.784 8.157 

And I want to get a list of sub_level index values 'STK_ID' , which will return the list ['000568','000596','000799'] .
Is there any direct function for this (without using reset_index and getting the column value)?

+4
source share
1 answer

You are looking for index.levels :

 In [10]: df1.index.levels Out[10]: [Index(['000568', '000596', '000799'], dtype=object), Int64Index([20120630, 20120930], dtype=int64)] In [11]: df1.index.levels[0] Out[11]: Index(['000568','000596','000799'], dtype=object) 

Note that you can see index names:

 In [12]: df1.index.names Out[12]: ['STK_ID', 'RPT_Date'] 

They are discussed in the docs here .

+8
source

All Articles