The Problem
Real data moment
What are the 10 most expensive line items in the real Chipotle dataset?
import pandas as pd
df = pd.read_csv("chipotle.tsv", sep="\t")
df["item_price"] = df["item_price"].str.replace("$", "", regex=False).astype(float)
top10 = df.sort_values("item_price", ascending=False).head(10)
print(top10[["order_id", "quantity", "item_name", "item_price"]])
order_id quantity item_name item_price
3598 1443 15 Chips and Fresh Tomato Salsa 44.25
3480 1398 3 Carnitas Bowl 35.25
1254 511 4 Chicken Burrito 35.00
3602 1443 4 Chicken Burrito 35.00
3601 1443 3 Veggie Burrito 33.75
409 178 3 Chicken Bowl 32.94
1255 511 3 Steak Burrito 27.75
3603 1443 3 Steak Burrito 27.75
3636 1454 3 Chicken Burrito 26.25
3634 1454 3 Chicken Burrito 26.25
The single most expensive line in the entire real dataset isn't a fancy bowl or burrito — it's Chips and Fresh Tomato Salsa, at $44.25, because quantity is 15. Exactly the line-total-vs-unit-price distinction from Phase 00: a $2.95 per-unit item, ordered 15 times on one line, outranks every single-item entrée in the dataset.
The gotcha
Look again at the very first result in this lesson — the row labels on the left aren't 0, 1, 2, ...:
print(top10.index.tolist())
[3598, 3480, 1254, 3602, 3601, 409, 1255, 3603, 3636, 3634]
Those are each row's original position in the unsorted 4,622-row DataFrame — sort_values() rearranges which rows come first, it doesn't relabel them. This is the exact same index behavior from Phase 00's filtering lesson, showing up again in a new operation: sorting, like filtering, changes row order or row membership, never row identity.
This is a feature, not a missed cleanup step
Keeping the original index means you can always trace a sorted row back to exactly where it lived in the source data — useful for cross-referencing against another table, or just confirming you're looking at the row you think you are. If you genuinely want a fresh 0, 1, 2, ... index after sorting, .reset_index(drop=True) does that explicitly — a deliberate choice, not pandas' silent default.
df.sort_values("quantity", ascending=False).head(3) is run on the real Chipotle data. The result's index is [3598, 4152, 3887]. What does this tell you?
sort_values() rearranges rows by value, but never renumbers them — every index value you see after a sort is exactly that row's position in the original, unsorted DataFrame. 3598 being first here isn't a coincidence with the earlier price example: it's the same real, very-large chips-and-salsa order (quantity 15) topping quantity too, not just price — followed here by a real quantity-10 Bottled Water order and a real quantity-8 Side of Chips order.