Selection with a MultiIndex – Multiple levels
Things would not be that interesting if you could only select from the first level of a pd.MultiIndex
. Fortunately, pd.DataFrame.loc
will scale out to more than just the first level through the creative use of tuple arguments.
How to do it
Let’s recreate the pd.Series
from the previous section:
index = pd.MultiIndex.from_tuples([
("John", "Smith"),
("John", "Doe"),
("Jane", "Doe"),
("Stephen", "Smith"),
], names=["first_name", "last_name"])
ser = pd.Series(range(4), index=index)
ser
first_name last_name
John Smith 0
Doe 1
Jane Doe 2
Stephen Smith 3
dtype: int64
To select all records where the first index level uses the label "Jane"
and the second uses "Doe"
, pass the following tuple:
ser.loc[("Jane...