Selection with a MultiIndex – a DataFrame
A pd.MultiIndex
can be used both as a row index and a column index, and selection via pd.DataFrame.loc
works with both.
How to do it
Let’s create a pd.DataFrame
that uses a pd.MultiIndex
in both the rows and columns:
row_index = pd.MultiIndex.from_tuples([
("John", "Smith"),
("John", "Doe"),
("Jane", "Doe"),
("Stephen", "Smith"),
], names=["first_name", "last_name"])
col_index = pd.MultiIndex.from_tuples([
("music", "favorite"),
("music", "last_seen_live"),
("art", "favorite"),
], names=["art_type", "category"])
df = pd.DataFrame([
["Swift", "Swift", "Matisse"],
["Mozart", "T. Swift", "Van Gogh"],
["Beatles", "Wonder", "...