Pandas How To Uncategorized How to query multiindex

How to query multiindex

In Pandas, you can query data from a MultiIndex DataFrame by using the .loc[] method and specifying the indices for each level of the MultiIndex.

For example, consider the following MultiIndex DataFrame:

import pandas as pd
index = pd.MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 1), ("B", 2)])
df = pd.DataFrame({"col1": [1, 2, 3, 4], "col2": [10, 20, 30, 40]}, index=index)

You can query the values in the MultiIndex DataFrame by specifying the indices for each level:

result = df.loc[("A", 1), :]

The resulting result will be a DataFrame containing the row with index (“A”, 1):

col1 col2
A 1 1 10

You can also query a range of values by specifying a slice for each level:

result = df.loc[("A", slice(1, 2)), :]

The resulting result will be a DataFrame containing the rows with indices (“A”, 1) and (“A”, 2):

col1 col2
A 1 1 10
2 2 20

You can also query values by specifying the index level and label:

result = df.xs("A", level=0, axis=0)

The resulting result will be a DataFrame containing all rows with level 0 index “A”:

col1 col2
1 1 10
2 2 20
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post