Python | Pandas MultiIndex.names

MultiIndex.names Pandas MultiIndex.names returns level names in MultiIndex.

Syntax: MultiIndex.names

Example # 1: Use the MultiIndex.names attribute to find the level names in MultiIndex.

# import pandas as pd

import pandas as pd

 
# Create an array

array = [ [ 1 , 2 , 3 ], [ ` Sharon` , `Nick` , ` Bailey` ]]

 
# Print array

print (array)

Output:

Now let`s create a MultiIndex using this array

# Create MultiIndex

midx = pd.MultiIndex.from_arrays (array, names = ( `Number` , ` Names` < / code> ))

 
# Print MultiIndex

print (midx)

Output:

Now we will find the names of all levels in MultiIndex.

# Display level names in MultiIndex
midx.names

Output:

As we can see from the output, midx has two levels, and the level names — “Number” and “Names”.

Example # 2: Use the MultiIndex.names attribute to find the level names in a given MultiIndex.

# import pandas as pd

import pandas as pd

 
# Create an array

array = [[ 1 , 2 , 3 ], [ `Sharon` , `Nick` , ` Bailey` ], 

  [ `Doctor` , ` Scientist` , `Physicist` ]]

 
# Print array

print (array)

Output:

Now let`s create a MultiIndex using this array

# Create MultiIndex

midx = pd.MultiIndex.from_arrays (array, 

names = ( `Ranking` , `Names` , ` Profession` ))

 
# Print MultiIndex

print (midx)

Output:

Now we will find the names of all levels in MultiIndex.

# Display level names in MultiIndex
midx.names

Output:

As we can see from the output, midx has three levels, and the level names — “Number”, “Names” and “Profession”.