Python | Pandas MultiIndex.is_lexsorted ()



MultiIndex.is_lexsorted() Pandas MultiIndex.is_lexsorted() returns True if labels are lexicographically sorted. Otherwise, the function returns False indicating that the labels are not lexicographically sorted.

Syntax: MultiIndex.is_lexsorted ()

Parameters: None

Returns: boolean

Example # 1: Use MultiIndex.is_lexsorted () to check if MultiIndex labels are lexicographically sorted or not.

# import pandas as pd

import pandas as pd

  
# Create MultiIndex

midx = pd.MultiIndex.from_arrays ([[ `Networking` , `Cryptography` ,

  `Anthropology` , ` Science` ], 

[ 88 , 84 , 98 , 95 ]])

 
# Print MultiIndex

print (midx)

Output:

Now let`s check if the labels are sorted in MultiIndex lexicographically or not.

 

# check if labels are sorted or not
midx.is_lexsorted ()

Output:

As we can see in the output, the function returned false, indicating that the labels in MultiIndex are not lexicographically sorted.

Example # 2: Use MultiIndex.is_lexsorted () to check if MultiIndex labels are lexicographically sorted or not.

# import pandas as pd

import pandas as pd

 
# Create Mult iIndex

midx = pd.MultiIndex. from_arrays ([[ `Anthropology` , ` Cryptography`

`Networking` , `Science` ], 

[ 88 , 84 , 98 , 95 ]])

 
# Print MultiIndex

print (midx)

< p> Output:

Now let`s check if the labels are sorted in MultiIndex lexicographically or not.

# check if labels are sorted or not
midx. is_lexsorted ()

Output:

As we can see from the output, the function returned true, indicating that the labels in the MultiIndex are sorted lexicographically.