Sort and find the top priorities
head(3) does not find the biggest values; it just takes the first three rows. Sort first, then limit:
top = accounts.sort_values("balance", ascending=False).head(3)
ascending=False puts largest values first. To resolve ties deliberately, sort on more than one column:
top = accounts.sort_values(["balance", "account"], ascending=[False, True]).head(3)
This means highest balance first, then account name alphabetically for equal balances. If the question asks for top customers, group transactions by customer before ranking: a large single transaction is not necessarily the largest customer total.
reset_index(drop=True) makes a report's row numbers run from zero without adding the old index as a new column.
See it run
import pandas as pd scores = pd.DataFrame({"name": ["Ana", "Bo", "Cy", "Di"], "pts": [70, 95, 82, 88]}) ranked = scores.sort_values("pts", ascending=False) best = ranked.head(2) fresh = best.reset_index(drop=True)
Sort first, then cut. Notice the row labels travel with their rows.
Practice sort and find the top priorities
Exercise 1
The collections lead can call three customers today. Read balances.csv as accounts. Save top with account and balance for the three largest balances, descending, breaking equal balances by account alphabetically. Use a fresh row index.
Exercise 2
A buyer needs the two suppliers with the highest total spend, not the two biggest invoices. Load supplier_invoices.csv as invoices. Sum amount by supplier, rank totals descending (supplier ascending for ties), and save the first two as top_suppliers with columns supplier, amount and a fresh index.
Exercise 3
A maintenance planner needs the four oldest OPEN jobs. Read maintenance_queue.csv as jobs. Filter open jobs first, then sort waiting_days descending and job ascending for ties. Save oldest with job and waiting_days only, using a fresh index.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.