A MultiIndex lets you represent naturally nested or multi-dimensional categorical data, like a year and month, or a country and city, as a single hierarchical row, or column, index instead of flattening everything into one combined string label. It can be constructed several ways — from_tuples() for an explicit list of label combinations, from_product() for every combination of two or more separate lists, a Cartesian product, or automatically, as the result of operations like groupby() with multiple columns, or unstack().
1Understanding pd.MultiIndex
A MultiIndex lets you represent naturally nested or multi-dimensional categorical data, like a year and month, or a country and city, as a single hierarchical row, or column, index instead of flattening everything into one combined string label. It can be constructed several ways — from_tuples() for an explicit list of label combinations, from_product() for every combination of two or more separate lists, a Cartesian product, or automatically, as the result of operations like groupby() with multiple columns, or unstack().
Use pd.MultiIndex.from_product() with two lists to build every possible combination of two categorical dimensions automatically, instead of manually writing out every label pair yourself with from_tuples().
import pandas as pd
index = pd.MultiIndex.from_tuples([("2026", "Jan"), ("2026", "Feb"), ("2027", "Jan")], names=["year", "month"])
s = pd.Series([100, 150, 200], index=index)
print(s)2Practical Example
Here is a real-world application of pd.MultiIndex showing how it is used in production Pandas code.
import pandas as pd
index = pd.MultiIndex.from_product([["2026", "2027"], ["Q1", "Q2"]], names=["year", "quarter"])
print(index)3Best Practices
Follow these guidelines when working with pd.MultiIndex:
1. Use a MultiIndex to represent genuinely hierarchical or multi-dimensional categorical data, instead of flattening it into a single combined string label
2. Use from_product() instead of manually listing every combination with from_tuples(), when you want every combination of two or more separate lists
3. Use .xs(), cross-section, to select data at a specific level of a MultiIndex conveniently, instead of more verbose boolean indexing on the index's components
Tip: Use pd.MultiIndex.from_product() with two lists to build every possible combination of two categorical dimensions automatically, instead of manually writing out every label pair yourself with from_tuples().
import pandas as pd
index = pd.MultiIndex.from_tuples([("2026", "Jan"), ("2026", "Feb"), ("2027", "Jan")], names=["year", "month"])
s = pd.Series([100, 150, 200], index=index)
print(s)