pandas · Chapter 11 of 22
Sorting and describe
By the end of this chapter you can sort a table by one column or several, each in its own direction, with the missing values where you want them; take the top or bottom few rows with nlargest and nsmallest, ties included; get one number from a column with sum, mean, median, min, max, std, count and nunique; read describe for numbers and for text; count values with value_counts; and use describe as a first check of a new file. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Selecting and cleaning
- 4 tasks
- pandas
- In the all labs plan
The first 2 chapters of pandas are free. This one comes with the all labs plan.
Why you need this
The website's lines are clean now, and the questions have changed from "is this right" to "which ones" and "how much". Shabnam wants the designs that brought in the most money this year, so the weavers know what to put on their looms. Gopal packs city by city and wants the newest orders on top. Harpreet is setting next season's prices and wants the catalogue's prices in one small table: the lowest, the highest, the middle and the spread.
With what you know so far, "the three dearest products" goes back to NumPy and back to chapter 1's problem:
import numpy as np
import pandas as pd
catalogue = pd.read_csv("/workspace/data/products.csv")
print(np.sort(catalogue["mrp_rs"].values)[::-1][:3])
[34500 32800 31900]
Those are the prices, with no skus beside them: sorting the array forgot which product each one belonged to. You could ask NumPy for the order with argsort and pick rows by position with iloc, but sorting by category A to Z and then by price from the top, in one go, is already hard to get right that way. pandas sorts whole rows, keeps their labels, and has a one-word answer for the top few and for every summary number.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
top_skus.py: the best-selling skus, ties included
Write top_skus(sales, n) in top_skus.py. sales is Harpreet's year summary: one row per sku, labelled 0, 1, 2 and so on, with the columns sku, units (whole numbers) and revenue_rs (decimals: the rupees of that sku's website lines that were not cancelled). n is a whole number.
Return a DataFrame of the n rows with the highest revenue_rs, highest first, with all three columns and the rows' own labels from sales (not 0, 1, 2 again). A sku tied with the n-th one on revenue_rs is kept too, so the result can have more than n rows: for revenues 9891, 7497, 4198, 4198 and 2549, top_skus(sales, 3) has 4 rows. Rows tied on revenue_rs may come in either order.
The Check tries the year's summary with n of 10 and 3, and a small summary of 5 skus labelled 40 to 44, with n of 3 (two skus tied in third place), 2 and 1.
- Task 2
sort_lines.py: the lines by city, newest first
Write by_city_newest(lines) in sort_lines.py. lines is the website's lines as read_csv reads orders_site.csv, with the exact duplicate lines and the three test lines dropped and the cities cleaned to the 16 names (chapter 10), so the row labels are the file's own, with gaps. placed_at is text like 2025-10-01 11:55:03, which sorts in time order as text.
Return a DataFrame of every line with every column, in the input's column order, sorted by city from A to Z and, within one city, by placed_at with the newest first. The rows keep their own labels. Lines with the same city and the same placed_at (the lines of one order) may come in any order among themselves.
The Check tries the website's lines and a small table of 6 lines in 3 cities, labelled 7, 3, 9, 1, 5 and 2.
- Task 3
price_summary.py: the catalogue's prices in one table
Write price_summary(catalogue) in price_summary.py. catalogue is a table like products.csv, with whole-number columns cost_rs and mrp_rs. A product's margin is its mrp_rs minus its cost_rs.
Return a DataFrame with exactly three columns, cost_rs, mrp_rs and margin_rs, in that order, and the eight rows that describe gives, labelled count, mean, std, min, 25%, 50%, 75% and max, in that order, all decimal numbers: the statistics of the products' costs, MRPs and margins.
The Check tries the catalogue (products.csv), a catalogue of 3 products labelled by sku with the columns name, cost_rs and mrp_rs, and a catalogue of 1 product, whose std is a missing value (one number has no spread).
- Task 4 · interview question
Interview: line_count.py, describe's count
Harpreet's line_count(lines) reads the number of order lines from describe, and for the whole of orders_site.csv it returns 14114.0, a float, though the file has 14128 lines. Fix it so it returns the number of rows in the table, as a whole number (an int). Then write blank_qty(lines) in the same file: it returns how many lines have no qty (a missing value there), as a whole number, and 0 when every line has one.
The Check tries the website's lines as read_csv reads them, a table of 5 lines of which 2 have no qty, and a table of 5 lines that all have one.
Devika will ask: what does describe's count count, and why was it smaller than the number of lines?
What you will learn
- sort_values: the rows in the order of one column or several, each ascending or descending, with na_position for the missing values.
- sort_index: the rows back in the order of their labels.
- nlargest and nsmallest: the top or bottom n rows, and keep= for ties.
- The statistics methods: sum, mean, median, min, max, std, count and nunique, on a column and on a table.
- describe: the count, mean, spread and quartiles of numbers, and the count, unique, top and freq of text.
- value_counts: how many times each value appears, or its share.
More in Selecting and cleaning
Build "Sorting and describe" yourself
Kargha, a Varanasi handloom brand, sells through a website, a marketplace and a store. You make one true picture of its sales. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of pandas are free. This one comes with the all labs plan.