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
Get Learning pandas now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.