Ranking Complete Records
Introduction
Welcome back to Sorting, Unique Values, and Table Analysis with NumPy. Last time, we saw how sorting columns can separate values from the stores they describe. Now we’ll keep each record intact while ranking it: argsort gives us row positions, which we can use to move a full row and its store label together.
By the end of this lesson, you’ll be able to:
- Get row positions with
argsortand use them to reorder a table and its labels together - Reverse those positions for a highest-to-lowest ranking
- Rank stores by one column or by their total sales
Why We Need Positions, Not Sorted Values
A sorted column gives us values in order, but it does not tell us how to move each value’s whole row. For that, we need the row positions in sorting order. As you may recall from earlier courses, indexing a table with a list of positions gathers its rows in that order. NumPy’s argsort gives us exactly such a list: it returns the positions that would sort the values, rather than returning the sorted values themselves.
Getting the Order with argsort on One Column
Let’s use the same sales table as in the previous lesson. Each row represents a store, and the first column holds the sales value we want to rank. The expression sales[:, 0] selects that column; calling .argsort() gives us the row positions in ascending order by that value.
Here, order is [1, 2, 0]: row 1 has the smallest first-column value, followed by row 2, then row 0. The printed output is:
These are positions in the original table, not the sorted sales values. Next, we’ll use those positions to move complete records.
Moving Whole Rows and Labels Together
We can use the same order to index both sales and stores. As with the index-list technique from earlier courses, NumPy gathers items in the listed order. Indexing the table moves whole rows, while indexing the parallel label array moves the matching store names. This keeps every store attached to its original sales record.
The output shows the original rows intact, followed by the labels in the matching order:
For example, [7, 14, 19] is still South’s complete row. The same position order reorders the table and its labels, so their connection is preserved.
The diagram traces how order = [1, 2, 0] moves each complete record and its matching label.

