Change language

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.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically