
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy argsort() Function
The numpy.argsort() function is used to return the indices that would sort an array. This function perform an indirect sort along the given axis using the algorithm specified by the kind parameter.
The numpy.argsort() function returns an array of indices of the same shape as a that index data along the given axis in sorted order. This function is particularly useful when we need the sorting order but want to rearrange the array or other data manually.
The numpy.argsort() function raises a TypeError if the input is not a valid array-like object or contains elements that cannot be compared or sorted.
Syntax
Following is the syntax of the Python numpy.argsort() function −
numpy.argsort(arr, axis=-1, kind=None, order=None)
Parameters
Following are the parameters of the Python numpy.argsort() function −
- arr: The input array for which the indices of the sorted elements are to be computed.
- order (optional): This specifies the field order for structured arrays. For example, sorting by multiple fields in a structured array.
-
axis (optional): The axis along which to sort −
- -1 (default): Sorts along the last axis.
- 0: Sorts along columns (vertically).
- 1: Sorts along rows (horizontally).
-
kind (optional): This specifies the sorting algorithm. Options include −
- quicksort (default): Fast but not stable.
- mergesort: Stable and maintains the relative order of equal elements.
- heapsort: Memory-efficient but slower than 'quicksort'.
- stable: A stable sorting algorithm introduced in NumPy 1.15.
Return Type
This function returns an array of indices of the same shape as the input array that represents the sorted order of elements.
Example
Following is a basic example to find the indices that would sort a 1-dimensional NumPy array using the Python numpy.argsort() function −
import numpy as np my_array = np.array([25, 5, 15, 10]) sorted_indices = np.argsort(my_array) print("Original Array:", my_array) print("Indices of Sorted Array:", sorted_indices) print("Sorted Array:", my_array[sorted_indices])
Output
Original Array: [25 5 15 10] Indices of Sorted Array: [1 3 2 0] Sorted Array: [ 5 10 15 25]
Example : Sorting Along Axis
We can sort along a specific axis in a multi-dimensional array using the axis parameter. The following example demonstrates sorting rows (axis=1) and columns (axis=0) −
import numpy as np my_array = np.array([[8, 2, 7], [4, 9, 1]]) row_indices = np.argsort(my_array, axis=1) col_indices = np.argsort(my_array, axis=0) print("Original Array:\n", my_array) print("Indices of Row-wise Sort:\n", row_indices) print("Indices of Column-wise Sort:\n", col_indices)
Output
Original Array: [[8 2 7] [4 9 1]] Indices of Row-wise Sort: [[1 2 0] [2 0 1]] Indices of Column-wise Sort: [[1 0 1] [0 1 0]]
Example : Sorting Using Custom Data Type
The order parameter is used for structured arrays. Following is an example to find indices for sorting by specific fields −
import numpy as np my_array = np.array([(1, 'Zebra', 100), (2, 'Apple', 50), (3, 'Monkey', 75)], dtype=[('id', int), ('name', 'U10'), ('score', int)]) indices_by_name = np.argsort(my_array, order='name') print("Original Array:\n", my_array) print("Indices of Sorting by Name:\n", indices_by_name) print("Array Sorted by Name:\n", my_array[indices_by_name])
Output
Original Array: [(1, 'Zebra', 100) (2, 'Apple', 50) (3, 'Monkey', 75)] Indices of Sorting by Name: [1 2 0] Array Sorted by Name: [(2, 'Apple', 50) (3, 'Monkey', 75) (1, 'Zebra', 100)]