~ 4 min read

Using MultiIndex Function in Pandas for Hierarchical Indexing

By: Adam Richardson
Share:

Creating MultiIndex with from_tuples() method

Pandas allows you to create hierarchical indexing in your dataframes which is really cool when you have complex data structures. One way of creating a multi-index dataframe is using the from_tuples() method. The from_tuples() method creates a MultiIndex from a list of tuples which represent the multiple index levels.

Suppose you have a dataset where you want to track sales of different products for two different quarters. You can create a multi-index dataframe as shown below:

import pandas as pd

sales_data = [("Product A", "Q1", 100),
              ("Product A", "Q2", 200),
              ("Product B", "Q1", 150),
              ("Product B", "Q2", 250)]

df = pd.DataFrame(sales_data, columns=["Product", "Quarter", "Sales"])
multi_index = pd.MultiIndex.from_tuples(df[["Product", "Quarter"]].to_records(index=False))
df = df.set_index(multi_index)

In the above example, we have created a list of tuples sales_data, which represents the Sales data for two quarters for two products. We then create a DataFrame df out of this list of tuples.

Finally, we create a multi-index using the from_tuples() method, which takes column values of the DataFrame as tuples and sets them as multi-index levels.

The set_index() method is then used to set the created MultiIndex to the DataFrame.

So, the resulting DataFrame df will have a multi-level index that represents the Product and Quarter.

Now you can access the data using multiple levels. For example:

print(df.loc[("Product A", "Q1")])

This will output:

Sales    100
Name: (Product A, Q1), dtype: int64

Through the use of from_tuples() method, you can create a pandas DataFrame with a hierarchical index for exploring and analyzing complex datasets.

Retrieving data with the .loc() method

Once you have created a MultiIndex DataFrame, the next step is to retrieve data from it. Using .loc() method is a recommended way to select data from a pandas dataframe when working with a multi-index.

The .loc() method in Pandas is used to select rows and columns by label. It allows you to select data elements from a DataFrame by providing values for rows and columns, including the MultiIndex.

For example, if we want to access the sales data for “Product A” from the example used above, we would use the .loc() method as shown below:

print(df.loc[("Product A")])

This will output:

          Sales
Quarter
Q1          100
Q2          200

Similarly, if we want to access the sales data for “Q2” of both products:

print(df.loc[(slice(None), "Q2"), :])

This will output:

                  Sales
Product   Quarter
Product A Q2        200
Product B Q2        250

Here we have used the : to select all columns.

We can also conditionally filter the data like this:

print(df.loc[(slice(None), "Q2"), :][df.loc[(slice(None), "Q2"), :]["Sales"] > 225])

This will output:

                  Sales
Product   Quarter
Product B Q2        250

In this example, we use the :, slice(None) and df.loc[] to perform filtering conditionally.

Using .loc() method can be a powerful way to retrieve data from a dataframe with hierarchical indexing.

MultiIndexing columns using the .columns attribute

In addition to MultiIndexing rows, we can also MultiIndex columns in a Pandas DataFrame using the .columns attribute. MultiIndexing columns allows us to work with data that has complex data structures.

Suppose we have a DataFrame with sales data on two different products and for two different quarters. We can MultiIndex columns in the DataFrame as shown below:

import pandas as pd

sales_data = {"Product A": {"Q1": 100, "Q2": 200},
              "Product B": {"Q1": 150, "Q2": 250}}

df = pd.DataFrame(sales_data)
multi_index = pd.MultiIndex.from_product([["Sales"], df.columns])
df.columns = multi_index

Here we create a dictionary with the product as the key and quarter as sub key and sale quantity as value, then create a DataFrame df out of it.

We can then create a MultiIndex of columns by using the .from_product() method with the list of column names and the name of the Sales. Finally, we assign this MultiIndex to the columns of the DataFrame.

Now, we can retrieve sales data for different products and quarters as shown below:

print(df.loc["Sales", "Product A"])

This will output:

Q1    100
Q2    200
Name: (Sales, Product A), dtype: int64

Similarly, we can retrieve the data for different quarters of different products as shown below:

print(df.loc["Sales", ["Product A", "Product B"]])

This will output:

  Product A  Product B
Q1       100        150
Q2       200        250

MultiIndexing columns using the .columns attribute is a powerful way to work with complex data structures in Pandas DataFrames.

Summary

Mastering hierarchical indexing in pandas can be a game-changer for data analysis tasks. In this post, we explored how to create a MultiIndex with the from_tuples() method, how to retrieve data using the .loc() method, and how to MultiIndex columns using the .columns attribute. Employing these techniques can allow you to work with complex data structures more easily and boost your data analysis skills. MultiIndexing columns can especially help in cases where your data has complex structures. You should consider using these pandas features when working with complex datasets.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts