
- 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
Python - numpy.lexsort() Function
In Python, the numpy.lexsort() function is used to perform an indirect sort using multiple keys. It sorts the elements based on the last key, followed by the second last, and so on. This is particularly useful for lexicographic (dictionary-like) ordering of multi-dimensional data.
The primary use of numpy.lexsort() function is to order data where sorting by multiple criteria is required, such as sorting records in a structured dataset. It returns an array of indices that can be used to obtain the sorted data.
Syntax
Following is the syntax of the Python numpy.lexsort() function −
numpy.lexsort(keys)
Parameters
Following are the parameters of the Python numpy.lexsort() function −
- keys: A sequence of arrays, where each array represents a column of data. Sorting starts from the last key and progresses to the first.
Return Values
This function returns an array of indices that can be used to sort the data lexicographically.
Example
Following is a basic example of sorting data lexicographically using Python numpy.lexsort() function −
import numpy as np # Define two keys key1 = np.array([1, 2, 3, 1]) key2 = np.array([4, 3, 2, 1]) # Perform lexsort indices = np.lexsort((key2, key1)) print("Sorted Indices:", indices) # Use indices to sort data sorted_key1 = key1[indices] sorted_key2 = key2[indices] print("Sorted Data:", list(zip(sorted_key1, sorted_key2)))
Output
Following is the output of the above code −
Sorted Indices: [3 0 1 2] Sorted Data: [(1, 1), (1, 4), (2, 3), (3, 2)]
Example : Sorting Strings Lexicographically
The numpy.lexsort() function can also be used to sort strings by converting them to arrays. Below is an example where we sort names by last name followed by first name.
import numpy as np # Define arrays for first and last names first_names = np.array(['John', 'Alice', 'Bob']) last_names = np.array(['Smith', 'Doe', 'Smith']) # Perform lexsort indices = np.lexsort((first_names, last_names)) print("Sorted Names:", [f"{last_names[i]}, {first_names[i]}" for i in indices])
Output
Following is the output of the above code −
Sorted Names: ['Doe, Alice', 'Smith, Bob', 'Smith, John']
Example : Multi-level Sorting
Using numpy.lexsort(), you can perform multi-level sorting. The keys are sorted from the last array to the first, allowing nested sorting.
In the following example, we sort an array of numbers based on two criteria: secondary and primary keys −
import numpy as np # Define the keys primary_key = np.array([1, 1, 2, 2]) secondary_key = np.array([4, 3, 2, 1]) # Perform lexsort indices = np.lexsort((secondary_key, primary_key)) print("Sorted Indices:", indices)
Output
Following is the output of the above code −
Sorted Indices: [1 0 3 2]
Example : Sorting in Reverse Order
The numpy.lexsort() function can be combined with slicing to perform sorting in reverse order.
In the following example, we sort data in descending order by using reversed indices −
import numpy as np key1 = np.array([1, 2, 3, 1]) key2 = np.array([4, 3, 2, 1]) # Perform lexsort indices = np.lexsort((key2, key1)) # Reverse the indices reverse_indices = indices[::-1] print("Reverse Sorted Indices:", reverse_indices)
Output
Following is the output of the above code −
Reverse Sorted Indices: [2 1 0 3]
Example : Working with Multi-dimensional Data
The numpy.lexsort() function can be used to sort multi-dimensional data when columns are treated as keys.
Here, we sort a 2D array row-wise based on two columns −
import numpy as np data = np.array([[8, 7], [6, 5], [4, 3], [1, 2]]) # Transpose to use columns as keys keys = data.T # Perform lexsort indices = np.lexsort(keys) sorted_data = data[indices] print("Sorted Data:\n", sorted_data)
Output
Following is the output of the above code −
Sorted Data: [[1 2] [4 3] [6 5] [8 7]]