Flatten all levels of MultiIndex
In this method, we are going to flat all levels of the dataframe by using the reset_index() function.
Syntax:
dataframe.reset_index(inplace=True)
Note: Dataframe is the input dataframe, we have to create the dataframe MultiIndex.
Syntax:
MultiIndex.from_tuples([(tuple1),.......,(tuple n),names=[column_names])
Arguments:
- tuples are the values
- column names are the names of columns in each tuple value
Example:
In this example, we will create a dataframe along with multiIndex and display it in the python programming language.
Python3
import pandas as pd # create DataFrame multiIndex data = pd.MultiIndex.from_tuples([( 'Web Programming' , 'php' , 'sub1' ), ( 'Scripting' , 'python' , 'sub2' ), ( 'networks' , 'computer network' , 'sub3' ), ( 'architecture' , 'computer organization' , 'sub4' ), ( 'coding' , 'java' , 'sub5' )], names = [ 'Course' , 'Subject name' , 'subject id' ]) # create dataframe with student marks data = pd.DataFrame({ 'ravi' : [ 98 , 89 , 90 , 88 , 93 ], 'reshma' : [ 78 , 89 , 80 , 98 , 63 ], 'sahithi' : [ 78 , 89 , 80 , 98 , 63 ]}, index = data) # display data |
Output:
Now, we will flatten the index of all levels:
Python3
import pandas as pd # create DataFrame multiIndex data = pd.MultiIndex.from_tuples([( 'Web Programming' , 'php' , 'sub1' ), ( 'Scripting' , 'python' , 'sub2' ), ( 'networks' , 'computer network' , 'sub3' ), ( 'architecture' , 'computer organization' , 'sub4' ), ( 'coding' , 'java' , 'sub5' )], names = [ 'Course' , 'Subject name' , 'subject id' ]) # create dataframe with student marks data = pd.DataFrame({ 'ravi' : [ 98 , 89 , 90 , 88 , 93 ], 'reshma' : [ 78 , 89 , 80 , 98 , 63 ], 'sahithi' : [ 78 , 89 , 80 , 98 , 63 ]}, index = data) # flatten the index of all levels data.reset_index(inplace = True ) # display data |
Output:
How to Flatten MultiIndex in Pandas?
In this article, we will discuss how to flatten multiIndex in pandas.
Contact Us