Can you explain the concept of a MultiIndex (hierarchical indexing) in Pandas?

Quick Overview

Explain what a pandas MultiIndex is and how hierarchical labels on rows or columns work. The question tests whether you can create one, select data by partial key, full key or cross-section, and reshape or aggregate a DataFrame by index level.

Can you explain the concept of a MultiIndex (hierarchical indexing) in Pandas?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Can you explain the concept of a MultiIndex (hierarchical indexing) in Pandas? You often get one back from a `groupby` on several keys or from a `pivot_table`, so be ready to say how it is structured and how you work with it. ```hint Think in tuples Picture every row label as a tuple, then ask what `.loc` should return when you pass only the first element of that tuple. ``` ### What a Strong Answer Covers - What a MultiIndex is: tuple labels on one axis, with ordered and named levels - The usual ways to create one - Selecting by a partial key, by a full key, and by a cross-section on an inner level - How levels work with `stack`/`unstack` and `groupby(level=...)` - Practical pitfalls, such as an unsorted index and tuples versus lists inside `.loc` ### Follow-up Questions - How would you select a range of years for every country without listing the countries? - How do you flatten the MultiIndex columns that `agg` produces when you pass several functions? - When would you call `reset_index()` and work with flat columns instead? - How do you merge or join a MultiIndexed DataFrame with another table?

Overview: Explain what a pandas MultiIndex is and how hierarchical labels on rows or columns work. The question tests whether you can create one, select data by partial key, full key or cross-section, and reshape or aggregate a DataFrame by index level.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Can you explain the concept of a MultiIndex (hierarchical indexing) in Pandas? You often get one back from a groupby on several keys or from a pivot_table, so be ready to say how it is structured and how you work with it.

What a Strong Answer Covers Guidance

  • What a MultiIndex is: tuple labels on one axis, with ordered and named levels
  • The usual ways to create one
  • Selecting by a partial key, by a full key, and by a cross-section on an inner level
  • How levels work with stack / unstack and groupby(level=...)
  • Practical pitfalls, such as an unsorted index and tuples versus lists inside .loc

Follow-up Questions Guidance

  • How would you select a range of years for every country without listing the countries?
  • How do you flatten the MultiIndex columns that agg produces when you pass several functions?
  • When would you call reset_index() and work with flat columns instead?
  • How do you merge or join a MultiIndexed DataFrame with another table?