To slice the outer index level of a pandas DataFrame or Series with a multi-level index, you can use the .loc method with a tuple of slice objects. The slice objects define the range of values you want to select for each level of the index.
Here's an example:
import pandas as pddata = {'Value': [10, 20, 30, 40, 50, 60], 'Year': [2019, 2019, 2019, 2020, 2020, 2020], 'Quarter': ['Q1', 'Q2', 'Q3', 'Q1', 'Q2', 'Q3']}df = pd.DataFrame(data)df.set_index(['Year', 'Quarter'], inplace=True)# Select all rows where the Year is 2019subset = df.loc[(2019, slice(None)), :]print(subset) |
In this example, the df DataFrame is created with a multi-level index composed of the Year and Quarter columns. We then set this index using set_index(). To slice the outer index level and select all rows where the Year is 2019, we pass a tuple containing (2019, slice(None)) to .loc. The slice(None) is used to select all possible values for the Quarter level of the index.
The resulting subset DataFrame is:
ValueYear Quarter 2019 Q1 10 Q2 20 Q3 30 |
Note that we selected all columns in this example using the : slice notation. You can also select specific columns by passing a list of column names or positions to .loc.