Hierarchical indexing is a feature of pandas that allows specifying two or more index levels on an axis. The specification of multiple levels in an index allows for efficient selection of subsets of data. A pandas index that has multiple levels of hierarchy is referred to as a MultiIndex
.
We can demonstrate creating a MultiIndex
using the sp500
data. Suppose we want to organize this data by both the Sector
and Symbol
. We can accomplish this with the following code:
In [107]: # first, push symbol into a column reindexed = sp500.reset_index() # and now index sp500 by sector and symbol multi_fi = reindexed.set_index(['Sector', 'Symbol']) multi_fi Out[107]: Price BookValue Sector Symbol Industrials MMM 141.14 26.668 Health Care ABT 39.60 15.573 ABBV 53.95 2.954 Information Technology ACN 79.79 ...