Pandasdrill

Level 4: Combining and reshaping Exercise 35 of 45 Pro

Columns into rows with stack

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

`wide` is indexed by store with one column per quarter. Use `stack` to turn it into a Series indexed by store and quarter.

What this exercise teaches

stack pivots the columns into the index, giving one value per store and quarter. unstack does the opposite, and calling it on a DataFrame stacks the other way round, with quarter first.

A hint if you get stuck

stack moves the column labels into the innermost level of the index.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Combining and reshaping