Flatten specific levels of MultiIndex
By using specific levels we can get by using the following syntax:
dataframe.reset_index(inplace=True,level=['level_name'])
where
- dataframe is the input dataframe
- level_name is the name of the multiindex level
Example:
In this example, we will create a dataframe and flatten specific levels of 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) # flatten the index of level with course column data.reset_index(inplace = True , level = [ 'Course' ]) # display data |
Output:
We can also specify multiple 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 level with course # and subject id columns data.reset_index(inplace = True , level = [ 'Course' , 'subject id' ]) # display data |
Output:
How to Flatten MultiIndex in Pandas?
In this article, we will discuss how to flatten multiIndex in pandas.
Contact Us