Skip to content
Related Articles

Related Articles

Improve Article
Python | Pandas MultiIndex.levshape
  • Last Updated : 24 Dec, 2018

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

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

Syntax: MultiIndex.levshape

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




# importing pandas as pd
import pandas as pd
  
# Creating the array
array =[[1, 2, 3], ['Sharon', 'Nick', 'Bailey']]
  
# Print the array
print(array)

Output :



Now let’s create the MultiIndex using this array




# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, names =('Number', 'Names'))
  
# Print the MultiIndex
print(midx)

Output :

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




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

Output :

As we can see in the output, the length of each levels in the midx MultiIndex is (3, 3).
 
Example #2: Use MultiIndex.levshape attribute to find the length of each levels in the given MultiIndex.




# importing pandas as pd
import pandas as pd
  
# Creating the array
array = [[1, 2, 3], ['Sharon', 'Nick', 'Bailey'],
                   ['Doctor', 'Scientist', 'Physicist']]
  
# Print the array
print(array)

Output :

Now let’s create the MultiIndex using this array




# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, names = ('Ranking', 'Names', 'Profession'))
  
# Print the MultiIndex
print(midx)

Output :

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




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

Output :

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :