Create a DataFrame with levels of MultiIndex as columns and substitute index level names in Python Pandas

Learn, how to create a DataFrame with the levels of the MultiIndex as columns and substitute index level names in Python Pandas? By IncludeHelp Last updated : April 10, 2023

MultiIndex Object

In Python Pandas, the MultiIndex object is the hierarchical analogue of the standard Index object which typically stores the axis labels in pandas objects. You can consider that MultiIndex is an array of unique tuples.

The pandas.MultiIndex.from_arrays() method is used to create a MultiIndex, and the names parameter is used to set names of each of the index levels.

Read: Create a MultiIndex with the names of each of the index levels

Create a DataFrame with levels of MultiIndex as columns and substitute index level names

To create a DataFrame with the levels of the MultiIndex as columns, pandas.MultiIndex.to_frame() method is used. This method accepts two parameters, index which is a bool type having default value True, it sets the index of returned DataFrame constructor with data as a dict. The second parameter is the name (optional) which is a list or sequence of str, the passed names should substitute index level names. The method returns a DataFrame containing the original MultiIndex data.

Syntax

MultiIndex.to_frame(index=True, name=NoDefault.no_default)

To work with MultiIndex in Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Python program to create a DataFrame with the levels of the MultiIndex as columns

# Import the pandas package
import pandas as pd

# Create arrays
employees = [
        ['E101', 'E102', 'E102', 'E103'],
        ['Alex', 'Alvin', 'Deniel', 'Jenny'],
    ]

# create a Multiindex using  from_arrays() 
mi = pd.MultiIndex.from_arrays(employees, names=('emp_id', 'name'))

# display the Multiindex
print("The MultiIndex...\n",mi)
print()

# Get the levels in MultiIndex
print("The levels in MultiIndex...\n",mi.levels)
print()

# Create a DataFrame
df = mi.to_frame(name=['Emp. ID', 'Emp. Name'])

# Print the DataFrame
print("The DataFrame is...\n", df)

Output

The MultiIndex...
 MultiIndex([('E101',   'Alex'),
            ('E102',  'Alvin'),
            ('E102', 'Deniel'),
            ('E103',  'Jenny')],
           names=['emp_id', 'name'])

The levels in MultiIndex...
 [['E101', 'E102', 'E103'], ['Alex', 'Alvin', 'Deniel', 'Jenny']]

The DataFrame is...
               Emp. ID Emp. Name
emp_id name                    
E101   Alex      E101      Alex
E102   Alvin     E102     Alvin
       Deniel    E102    Deniel
E103   Jenny     E103     Jenny

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.