Python | Pandas Index.argsort ()



Index.argsort() Pandas Index.argsort() returns integer indexes which will sort the index … By default, the sort order was set in ascending order.

Syntax: Index.argsort (* args, ** kwargs)

Parameters:
* args: Passed to numpy.ndarray.argsort
** kwargs: Passed to numpy.ndarray.argsort

Returns: numpy.ndarray
Integer indices that would sort the index if used as an indexer

Example # 1: Use Index.argsort () to find the order of indexes that will sort a given index.

# import pandas as pd

import pandas as pd

 
# Create index

df = < code class = "plain"> pd.Index ([ 17 , 69 , 33 , 5 , 10 , 74 , 10 , 5 ])

 
# Print index
df

Output:

Let`s find the order of the indexes that will sort the index.

# find and the index order
# which would sort the index df
df.argsort ()

Output:

As we can see in the output, the function returned the index order that would sort the given index. We can verify this by printing the Order Based Index.

# Print index based on
# result of argsort () function
df [df.argsort ()]

Output:

As we can see in the output, it is printed in sorted order.

Example # 2: Use Index.argsort () to find the order of the indexes that will sort the given index.

# import pandas as pd

import pandas as pd

  
# Create index

df = pd.Index ([ `Sam` , `Alex` , ` Olivia` ,

`Dan` , `Brook` , ` Katherine` ])

  
# Print index
df

Output:

Let`s find the order of the indexes that will sort the index.

# find index order
# which would sort the index df
df.argsort ()

Output:

As we can see in the output, the function returned the index order, which would sort the given index. We can verify this by printing the Order Based Index.

# Print index based on
# result of argsort () function
df [df.argsort ()]

Output:

As we can see in the output, it is printed in sorted order.