What do pandas stack() and unstack() do with a MultiIndex?

Quick Overview

Asks what the pandas stack() and unstack() methods do to a DataFrame or Series with a MultiIndex. Tests whether you can predict which level moves, the shape and type of the result, and how missing combinations are handled when reshaping between wide and long formats.

What do pandas stack() and unstack() do with a MultiIndex?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

In pandas, what do `stack()` and `unstack()` do with a `MultiIndex`? Describe which way each method moves data between the row index and the columns, and what the result looks like. ```hint Think in levels Treat the row index and the column index as two lists of levels. For each method, ask which level leaves one axis and where it lands on the other. ``` ### What a Strong Answer Covers - The direction of each method: which axis loses a level, which axis gains one, and whether the data gets wider or longer. - Which level moves by default and how to pick another by position or name. - How missing combinations are handled in each direction, including NaN fill, `fill_value`, and dtype changes. - When the result is a Series and when it is a DataFrame. - A practical use, such as turning one key of a multi-key `groupby()` result into columns. ### Follow-up Questions - How does `unstack()` relate to `pivot()` and `pivot_table()`, and why does it sometimes fail with "Index contains duplicate entries"? - If you call `unstack()` and then `stack()` on the same level, do you always get the original object back? What can differ? - How is `melt()` different from `stack()` when you turn a wide table into a long one? - What happens to memory when you unstack a level with thousands of distinct values, each present for only a few rows?

Overview: Asks what the pandas stack() and unstack() methods do to a DataFrame or Series with a MultiIndex. Tests whether you can predict which level moves, the shape and type of the result, and how missing combinations are handled when reshaping between wide and long formats.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

In pandas, what do stack() and unstack() do with a MultiIndex? Describe which way each method moves data between the row index and the columns, and what the result looks like.

What a Strong Answer Covers Guidance

  • The direction of each method: which axis loses a level, which axis gains one, and whether the data gets wider or longer.
  • Which level moves by default and how to pick another by position or name.
  • How missing combinations are handled in each direction, including NaN fill, fill_value , and dtype changes.
  • When the result is a Series and when it is a DataFrame.
  • A practical use, such as turning one key of a multi-key groupby() result into columns.

Follow-up Questions Guidance

  • How does unstack() relate to pivot() and pivot_table() , and why does it sometimes fail with "Index contains duplicate entries"?
  • If you call unstack() and then stack() on the same level, do you always get the original object back? What can differ?
  • How is melt() different from stack() when you turn a wide table into a long one?
  • What happens to memory when you unstack a level with thousands of distinct values, each present for only a few rows?