Python | Pandas MultiIndex.from_arrays ()



MultiIndex.from_arrays() Pandas MultiIndex.from_arrays() is used to convert arrays to MultiIndex. This is one of several ways we create MultiIndex.

Syntax: MultiIndex.from_arrays (arrays, sortorder = None, names = None)

Parameters:
arrays: Each array-like gives one level`s value for each data point. len (arrays) is the number of levels
sortorder: Level of sortedness (must be lexicographically sorted by that level)

Returns: index: MultiIndex

Example # 1: Use MultiIndex.from_arrays () to create MultiIndex from arrays.

# 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:

As we can see in the output, the function has created a MultiIndex object using arrays .

Example # 2: Use MultiIndex.from_arrays () to create MultiIndex from arrays.

# import pandas as pd

import pandas as pd

 
# Create 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:

As we can see in the output, the function has created a MultiIndex using the supplied arrays.