Hierarchical indexing
Hierarchical indexing is a feature of pandas that allows the combined use of two or more indexes per row. Each of the indexes in a hierarchical index is referred to as a level. The specification of multiple levels in an index allows for efficient selection of different subsets of data using different combinations of the values at each level. Technically, a pandas index that has multiple levels of hierarchy is referred to as a MultiIndex
.
The following code demonstrates creating and accessing data via a MultiIndex
using the sp500
data. Suppose we want to organize this data by both the values of Sector
and Symbol
so that we can efficiently look up data based on a combination of values from both variables. We can accomplish this with the following code, which moves the Sector
and Symbol
values into a MultiIndex
:

The .index
property now shows that the index is a MultiIndex
object:

As stated, a MultiIndex
object contains two or more levels, two in this case:

Each level is a...