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 ...

Get Learning pandas - Second Edition 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.