Python | Pandas MultiIndex.levshape

MultiIndex.levshape Pandas MultiIndex.levshape outputs a tuple containing the length of each level in MultiIndex.

Syntax: MultiIndex.levshape

Example # 1: Use the MultiIndex.levshape attribute to find the length of each level in MultiIndex.

# import pandas as pd

import pandas as pd

 
# Create 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` ))

 
# Print MultiIndex

print (midx)

Output:

Now we will find the length of each level in MultiIndex.

# Print the length of each level in MultiIndex
midx.levshape

Output:

As we can see in the output, the length of each level in the MultiIndex is (3, 3).

Example # 2: Use Attribe ut MultiIndex.levshape to find the length of each level in the given MultiIndex.

# import pandas as pd

import pandas as pd

 
# Create an array

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

  [ `Doct or` , `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 length of each level in MultiIndex.

# Print the length of each level in MultiIndex
midx.levshape

Output:

As we can see in the output, the length of each level is in midx equal to 3.