Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 64 ยท Selecting and Filtering

MultiIndex

Create hierarchical indexes with set_index, from_tuples and from_product, select with loc, xs and slices, sort levels, swap them, and flatten back to columns.

A normal index gives each row one label. A MultiIndex gives each row several labels arranged in levels, such as country then city, or year then month. It lets one two-dimensional table hold data that is really grouped, and it is what groupby produces when you group by more than one column. You will meet it whether you plan to or not, so it pays to know how to read it, select from it and, when you prefer, flatten it away.

This lesson builds on The Index. You will create a MultiIndex three ways, select with loc, xs and slices, sort and reorder levels, and convert it back to ordinary columns.

What a MultiIndex looks like

Think of a MultiIndex as a tuple of labels per row. The first level is the outer one; repeated outer labels are shown once, in a grouped layout.

flowchart TD
    I["MultiIndex<br/>(country, city)"] --> L1["Level 0: country"]
    I --> L2["Level 1: city"]
    L1 --> N1["DE"]
    L1 --> N2["FR"]
    N1 --> C1["Berlin"]
    N1 --> C2["Munich"]
    N2 --> C3["Lyon"]
    N2 --> C4["Paris"]

Each row is addressed by a path through the levels, such as DE then Munich.

Creating a MultiIndex

The most common way is set_index with a list of columns. You can also build the index first from tuples (from_tuples), from separate lists that are crossed (from_product), or from parallel arrays (from_arrays).

Python Three ways to create one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

from_product is handy for building a complete grid of combinations to reindex against, so that missing combinations show up as NaN instead of silently disappearing.

Selecting with loc

Pass a tuple to loc, one entry per level. A single outer label returns all rows beneath it; a full tuple returns one row.

Python Selecting rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what happens to a partial selection such as df.loc["FR"]: the outer level disappears from the result because every row would carry the same value. Use df.loc[["FR"]] (a list) if you want to keep it.

Slices need a sorted index

Selections that mix levels work with slice(None) meaning "everything at this level" (or pd.IndexSlice, a nicer way to write the same thing). Label ranges such as "east":"north" require the index to be sorted; otherwise pandas raises an UnsortedIndexError. Call sort_index() first.

Python Slicing with IndexSlice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Inside idx[...] a colon alone means "all labels". Label ranges such as "east":"north" are inclusive, as with any loc slice (see loc and iloc).

xs: a cross-section

xs(key, level=...) takes a label from any level, not just the outer one, which is exactly what bare loc cannot do concisely. By default the chosen level is removed from the result; drop_level=False keeps it.

Python xs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sorting, swapping and reordering levels

Many operations assume the index is sorted. sort_index() sorts by the levels from outer to inner; level= sorts by one level. swaplevel exchanges two levels (the outer one becomes inner), and reorder_levels sets a full new order.

Python Sorting and swapping levels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Levels, labels and changing level names

The index remembers the distinct values of each level in .levels. That list can contain values that no row uses any more (after a filter), which is a classic source of confusion: use get_level_values for what is really present, and remove_unused_levels to clean up.

Python Inspecting levels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Flattening back to columns

You can turn the levels back into columns with reset_index(), or only some of them with reset_index(level=...). This is often the last step before plotting, merging or saving to CSV. Combined labels for a single flat index are easy to build as well.

Python Flattening

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When columns become a MultiIndex, usually after aggregating with several functions, flatten them with a list comprehension such as df.columns = ["_".join(col) for col in df.columns]. agg, transform and filter and Pivot Tables and Crosstabs show where those appear.

If a MultiIndex gets in your way, you can nearly always reset_index() and work with ordinary columns. Reach for a MultiIndex when it simplifies selection of nested groups, not as the default.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you create a MultiIndex from the columns country and city?

    Pass a list of column names to set_index; each column becomes one level, outermost first.
  2. 2. Which call returns the rows where the inner level region equals 'south', whatever the outer level?

    xs selects a label on any level. Plain loc['south'] would look for 'south' in the outer level.
  3. 3. What error do you risk when slicing across levels on an unsorted MultiIndex?

    Label-based slicing needs a lexicographically sorted index. Call sort_index() first.
  4. 4. After df.loc[['a', 'b']] removed all rows of group 'c', what does df.index.levels[0] still contain?

    Levels keep all original values. Use get_level_values(...).unique() for what is actually present, or remove_unused_levels().

Summary

  • A MultiIndex gives each row several labels; create it with set_index([...]), from_tuples, from_product or from_arrays.
  • loc takes a tuple with one entry per level; a partial outer selection drops that level from the result.
  • Slice across levels with slice(None) or pd.IndexSlice, after sort_index().
  • xs(key, level=...) selects on any level; drop_level=False keeps it.
  • swaplevel, reorder_levels and sort_index(level=...) rearrange levels.
  • .levels can hold unused values; use get_level_values or remove_unused_levels.
  • reset_index() flattens the levels into columns when you want a simpler table.

Next up: change what your columns are made of in Data Types and Conversion.