O'Reilly logo

Learning pandas by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hierarchical indexing

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required