
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns and substitute index level names
To create a DataFrame with the levels of the MultiIndex as columns, use the MultiIndex.to_frame() method. Substitute index level names using the name parameter.
At first, import the required libraries −
import pandas as pd
MultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −
arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]
The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −
multiIndex = pd.MultiIndex.from_arrays(arrays)
Create a DataFrame with the levels of the MultiIndex as columns using to_frame(). Use the "name" parameter and pass the names to substitute index level names −
dataFrame = multiIndex.to_frame(name=['One', 'Two'])
Example
Following is the code −
import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects # Create arrays arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']] # The "names" parameter sets the names for each of the index levels # The from_arrays() is used to create a MultiIndex multiIndex = pd.MultiIndex.from_arrays(arrays) # display the MultiIndex print("The Multi-index...\n",multiIndex) # get the levels in MultiIndex print("\nThe levels in Multi-index...\n",multiIndex.levels) # Create a DataFrame with the levels of the MultiIndex as columns using to_frame() # Use the "name" parameter and pass the names to substitute index level names dataFrame = multiIndex.to_frame(name=['One', 'Two']) # Display the DataFrame print("\nThe DataFrame...\n",dataFrame)
Output
This will produce the following output −
The Multi-index... MultiIndex([(1, 'John'), (2, 'Tim'), (3, 'Jacob'), (4, 'Chris')], ) The levels in Multi-index... [[1, 2, 3, 4], ['Chris', 'Jacob', 'John', 'Tim']] The DataFrame... One Two 1 John 1 John 2 Tim 2 Tim 3 Jacob 3 Jacob 4 Chris 4 Chris
- Related Articles
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns but avoid setting the index of the returned DataFrame
- Python Pandas - How to create a MultiIndex with names of each of the index levels
- Python Pandas - Return MultiIndex with multiple levels removed using the level names
- Python Pandas - Get the Names of levels in MultiIndex
- Python Pandas - Create Multiindex from dataframe
- Python – Drop multiple levels from a multi-level column index in Pandas dataframe
- Python Pandas - Rearrange levels using level name in MultiIndex
- Python – Remove multiples levels using the level names and return the index
- Python Pandas - Create a DataFrame with both the original index and name
- Python Pandas - Swap levels of a MultiIndex
- Python Pandas - Get the levels in MultiIndex
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- Python Pandas - Set levels in a MultiIndex
- Python Pandas - Return MultiIndex with requested level removed using the level name

Advertisements