Pandas/Merging & reshaping 8 min

MultiIndex basics

Work with grouped results that have more than one key.

multiindexunstackxs

Grouping by two keys produces a MultiIndex — one index level per key. It is not a different object, just an index with tuples.

python · editable
loading editor…

Select a level with .loc and a tuple, or use xs to slice on an inner level directly.

python · editable
loading editor…

Try it: Get the revenue for UK Hardware only.

unstack pivots an index level into columns and reset_index flattens everything back into plain columns — usually the last step before charting or exporting.

python · editable
loading editor…