Sortrows with multiple sorting keys in numpy

34    Asked by lucy_5053 in Python , Asked on Jul 18, 2025

Want to sort a NumPy array by multiple columns, similar to sorting a spreadsheet by more than one column? Learn how to use NumPy's advanced indexing and argsort() to sort rows with multiple sorting keys efficiently.

Answered by Krista Goyette

Sorting rows with multiple sorting keys in NumPy is a common task when working with structured or tabular data. Think of it like sorting a spreadsheet first by one column (like last name), then by another (like first name). In NumPy, this is typically done using np.lexsort() or chained argsort() operations.

Here’s how you can do it in a clean and efficient way:

  •  Using np.lexsort():
  • np.lexsort() performs a stable sort using multiple keys.
  • The keys must be provided in reverse order of priority (i.e., the last key is the primary key).

Example:

import numpy as np
data = np.array([[1, 2, 3],
                 [1, 1, 2],
                 [2, 2, 1],
                 [1, 1, 1]])
# Sort by column 0 (then) column 1 (primary key)
sorted_data = data[np.lexsort((data[:,1], data[:,0]))]
print(sorted_data)

 Explanation:

  • data[:,1] → secondary key (sorts after the first)
  • data[:,0] → primary key (sorted first)

This will sort the rows primarily by column 0 and then by column 1.

 Benefits:

  • Works well for sorting by multiple columns in structured or 2D arrays.
  • np.lexsort() is optimized for performance with large datasets.

 Pro Tip:

  • If you're sorting strings or more complex types, consider using structured arrays with named fields for readability and flexibility.
  • Using np.lexsort() is the go-to solution when you need multi-key sorting in NumPy. It's fast, stable, and easy to integrate into data workflows.



Your Answer

Interviews

Parent Categories