Pandas · Sort and take the top rows
Google Search Analytics
Course overviewGoogle Search · Filter and sort searches

Sort and take the top rows

Sort ascending or descending, resolve ties, and take a reproducible top-N result.

Step 1 of 3 · Learn

Choose the sort direction

sort_values() sorts ascending by default. Set ascending=False to put larger values first, then call head(n) to keep the first n rows.

Break ties explicitly

When values tie, add another sort column. The ascending list follows the order of those columns; a unique search ID makes the final order predictable.

indexidscore
0320
1130
2230
3410
indexidscore
1130
2230
0320
Sort scores high to low, then tied IDs low to high. Take three rows after sorting.
▷ Your turn

Return search_id and response_ms for the ten slowest searches. Sort response_ms descending, breaking ties by search_id ascending. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.