Data Science
Hierarchical Indexing: The MultiIndex
SQL Mastery Team
May 19, 2026
6 min read
It's **Day 118**. Sometimes one index isn't enough. What if you have "Revenue" for every "State" and every "City" inside that state?
You need a **MultiIndex**.
How it's created
Usually, you get a MultiIndex after a complex GroupBy:
grouped = df.groupby(['state', 'city'])['revenue'].sum()
print(grouped)
Accessing Data
Getting data out of a MultiIndex requires `.loc` with a tuple:
# Get revenue for just New York City in NY State
nyc_rev = grouped.loc[('NY', 'New York City')]
Turning it back into a normal table
Analysts often hate MultiIndexes for plotting. You can "Flatten" them easily:
clean_df = grouped.reset_index()
Your Task for Today
Group a dataset by two columns and practice using `.loc` to pull data for a specific sub-category.
*Day 119: Using .map() and .replace().*