Hierarchical Indexing

Let’s learn about hierarchical and multilevel indexing of DataFrames in pandas.

We'll cover the following...

Hierarchical indexing is another very important feature of pandas. It makes it possible to have multiple (two or more) index levels on an axis. Putting it somewhat abstractly, it provides a way to work with higher-dimensional data in a lower-dimensional form.

This simple example helps to illustrate this point:

Press + to interact
# Importing numpy and Pandas first
import numpy as np
import pandas as pd
# Set a random seed for reproducibility
np.random.seed(42) # You can use any integer as the seed value
# Create a Series with a list of lists (or arrays) as the index:
index = [['a','a','a','b','b','b','c','c','d','d'], # level 1 index
[1,2,3,1,2,3,1,2,1,2]] # level 2 index
# Let's create a Series "ser" with multi-level index (2 in this example)
ser = pd.Series(np.random.randn(10), index=index)
print(ser)

With a hierarchically indexed object, we can carry out so-called partial indexing, which enables concise selection of subsets of the data. For ...