We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Sorting Data

Pandas provides a convenient .sort_values() method to sort a DataFrame by column values.

# Sort by a single column (ascending by default)
df_sorted = df.sort_values("price")

# Sort in descending order
df_sorted = df.sort_values("price", ascending=False)

You can get the top N rows by chaining .head() after sorting:

# 10 most expensive listings
priciest = df.sort_values("price", ascending=False).head(10)

# 5 cheapest listings
cheapest = df.sort_values("price").head(5)

Sorting by Multiple Columns

To sort by multiple columns, pass a list of column names. Pandas sorts by the first column, then breaks ties with the next:

# Sort by type first, then by price within each type
df_sorted = df.sort_values(["property_type", "price"])

# Mix ascending and descending
df_sorted = df.sort_values(
    ["property_type", "price"],
    ascending=[True, False],  # type A–Z, then price high–low
)

A second sort column is great for deterministic tie-breaking. If two listings have the same price, sorting by listing_id next guarantees the same order every time.

Assignment

SnackStack's field technicians can only service so many devices per shift. The support team wants a triage list: the n hottest devices first, so the riskiest units get attention soonest.

Complete the get_triage_list function. It accepts a DataFrame and a count n, and returns the top n devices to service.