0Pricing
Data Science Academy · Lesson

stack, unstack, and MultiIndex

Reshaping hierarchical tables.

stack, unstack, and MultiIndex is a free Data Science Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Reshaping With the Index

Beyond melt and pivot, pandas can reshape using the index itself. The tools are stack and unstack on hierarchical labels. 🏗️

What a MultiIndex Is

A MultiIndex stacks two or more label levels on one axis, so a single row can carry both a region and a year together.

stack Pushes Columns Down

stack moves the innermost column level into the row index, making the table taller and narrower in one move.

df.stack()

unstack Pulls Rows Up

unstack is the reverse: it lifts an index level back into columns, making the table wider and shorter again.

df.unstack()

They Undo Each Other

Stack then unstack returns the original shape. Think of them as a paired toggle between row labels and column labels.

Pick a Level to Move

Pass a level name or number to choose which one shifts. This level control lets you reshape exactly the axis you mean.

df.unstack(level='year')

Selecting From a MultiIndex

Use loc with a tuple to drill into nested labels, grabbing one region-year combination directly from the stacked index.

df.loc[('West', 2021)]

Cross-Section With xs

The xs method slices one level cleanly, like every row for year 2021, without unpacking the whole index by hand.

df.xs(2021, level='year')

Flatten When You Are Done

To return to plain columns, call reset_index. It turns every index level back into an ordinary, exportable column.

df.reset_index()

Stack Drops NaN by Default

By default stack quietly removes missing combinations. Set dropna to False when you need to keep every gap visible.

df.stack(dropna=False)

Where This Fits

groupby with several keys returns a MultiIndex, so unstack is the natural finishing move to spread those groups into a grid.

Quick Check

Last check: stacking and unstacking.

Recap: You Can Reshape Anything

You now wield pivot_table, melt, stack, and unstack. With layout under control, your analysis stays clean and flexible. 🎉

Frequently asked questions

Is the “stack, unstack, and MultiIndex” lesson free?

Yes — the full text of “stack, unstack, and MultiIndex” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “stack, unstack, and MultiIndex”?

Reshaping hierarchical tables. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “stack, unstack, and MultiIndex” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Wide vs Long, and Tidy Data
  2. pivot_table for Cross-Tabs
  3. melt to Go Long
  4. stack, unstack, and MultiIndex
← Back to Data Science Academy