Create an empty MultiIndex

Given a pandas dataframe, we have to create an empty MultiIndex. By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Multilevel Indexing

Multilevel indexing is a type of indexing that include different levels of indexes or simply multiple indexes. The DataFrame is classified under multiple indexes and the topmost index layer is presented as level 0 of the multilevel index followed by level 1, level 2, and so on.

Creating an empty MultiIndex

For this purpose, we will first create indices for multiindex DataFrame by using pandas.MultiIndex() method inside which we will pass multiple parameters like labels, and levels. Once we are done with indices, we will create a list of columns and give some column names in that list.

After creating the separate index and column list, we will create this empty DataFrame with multiple indices with the help of pandas.DataFrame() method.

Let us understand with the help of an example,

Python program to create an empty MultiIndex

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating indices
ind = pd.MultiIndex(levels=[[],[],[]],codes=[[],[],[]],names=['A', 'B', 'C'])

# Display index

# Creating column list
col = ['One','Two']

# Creting empty DataFrame
df = pd.DataFrame(index=ind,columns=col)

# Display DataFrame


The output of the above program is:

Example: Create an empty MultiIndex

Python Pandas Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.