NumPy: Sorting Arrays Using lexsort()

It is used to perform indirect sorting using a sequence of keys.

If multiple sorting keys can be interpreted as columns in a spreadsheet. Here, we can use lexsort() which returns an array of integer indices that describe the sorted order of multiple columns.
In this method, the last key in the sequence is used as the primary sorting order and the second-to-last key for secondary sort order and so on.

Syntax:

numpy.lexsort(keys, axis=- 1)

Examples:

marks= np.array([10,15,10,14,13,14]) # First column
n= np.array(['b','d','a','d','c','f']) # Second column
rank= np.lexsort((b,a)) # Sort by a, then by b
print(rank)

Output:

[2 0 4 3 5 1]
Tue, 09/07/2021 - 19:50

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.