How to set multiindex

In this post I will write how to set multiindex in Pandas.

You need a multiindex if you are able to point to two (or more) unique columns in your dataframe. Thanks to multiindex you will be able to manage your dataframe in an advanced way, which will look professional.

Creating a multiindex is a sign that you are a true professional and know Pandas at an advanced level.

How to set multiindex in Pandas

In order to create a multiindex I will use the set_index function, which is included in the Pandas library in Python.

This is my example dataframe which contains two columns that I will use as indexes.

import pandas as pd

my_df = pd.DataFrame({'Region': ['1001', '1001', '1002', '1002'],
                      'City': ['City1', 'City2', 'City3', 'City4'],
                      'Column1': [2, 5, None, 46],
                      'Column2': [12, None, 5, 22]})

print(f'My dataframe before I set the multiindex: \n{my_df}')

how to set multiindex

As you can see the dataframe contains a redundant ordinal column as a useless and unprofessional index.

I am using the set_index function to create a multiindex from Region and City columns.

import pandas as pd

my_df = pd.DataFrame({'Region': ['1001', '1001', '1002', '1002'],
                      'City': ['City1', 'City2', 'City3', 'City4'],
                      'Column1': [2, 5, None, 46],
                      'Column2': [12, None, 5, 22]})

print(f'My dataframe before I set the multiindex: \n{my_df}')

my_df.set_index(['Region', 'City'], drop=True, inplace=True)

print(f'My dataframe with the multiindex: \n {my_df}')

In addition, I removed the Region and City columns so that they do not appear twice in the dataframe. I also used the Inplace parameter to write the multiindex to the dataframe.

how to insert multiindex in Pandas

Admit that the dataframe looks clearer and more professional.

In addition, you can also check the details of your multiindex with the command

print(my_df.index)

index info

See also:
Documentation of set_index method

This Post Has One Comment

Leave a Reply