

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Multiple Conditions
incomplete
2: The Not Operator
incomplete
3: Filter Methods
incomplete
4: Binning
incomplete
5: String Operations
incomplete
6: Filtering With String Methods
incomplete
7: Sorting Data
incomplete
8: More Sorting
incomplete
9: Conditional Updates
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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)
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.
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.