pandas · Chapter 7 of 22
New and changed columns
By the end of this chapter you can add a column to a DataFrame from other columns or from one value, change a column or only some of its rows, sort values into bands with np.where, pd.cut and pd.qcut, rename and drop columns, and say what copy-on-write in pandas 3 means for a table you change. 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 files Kargha gets have the columns their senders chose, not the ones Kargha needs. Harpreet pays the weavers from what each order line really brought in: the price times the quantity, less the coupon. No file has that number. Shabnam wants every product in a price band, so the website can show "everyday", "festive" and "bridal" pages. Harpreet also wants the marketplace's orders next to the website's, and the two files do not even call their columns the same thing (Order Id against order_id). And Gopal, told that a few pieces were lost in transit, set their counts to 0 with a line of pandas that printed a long warning and changed nothing.
With what you know so far you can compute the amount as a Series, lines["price_rs"] * lines["qty"], but it lives outside the table: it is not printed with the lines, not kept when you filter them, and has to be computed again every time. You need to put it in the table, and to know when changing a table changes something else.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
line_amounts.py: each line's amount after the coupon
Write with_amount(lines) in line_amounts.py. lines is the website's lines, as read_csv reads orders_site.csv, with two changes made first: the lines with a blank qty are left out, and a blank discount_rs is 0, so qty, price_rs and discount_rs have no blanks.
Return a new DataFrame with every column of lines, in the same order, and one more column at the end, line_rs, which is price_rs times qty less discount_rs (the coupon on the whole line), as decimal numbers: for 2 pieces at 1499.0 with a coupon of 299.8 it is 2698.2. The rows and their labels are the same as in lines.
with_amount must not change the table it is given: the Check passes a table and then looks at it again. The Check tries the website's lines and a small table of 3 lines labelled 5, 6 and 9.
- Task 2
price_bands.py: a price band for every product
Write price_bands(catalogue, edges, names) in price_bands.py. catalogue is the catalogue, as read_csv reads products.csv, or a smaller table with the columns sku and mrp_rs; edges is a list of prices from low to high; names is a list of band names, one fewer than the edges.
A band runs from one edge up to the next, and a price exactly on an edge goes in the band above it: with the edges [0, 2000, 10000, 40000] and the names ["everyday", "festive", "bridal"], 1999 is everyday, 2000 is festive and 10000 is bridal. Every price is at least the first edge and under the last one.
Return a DataFrame with the columns sku, mrp_rs and band, in this order, one row per product, with the catalogue's own labels, in its order. band is the band's name, as text or as the category that pd.cut gives (either is accepted).
The Check tries the catalogue with the edges and names above, the catalogue with [0, 1000, 5000, 20000, 35000] and ["budget", "classic", "premium", "heirloom"], and a small table of 4 products labelled by sku, with prices of 1999, 2000, 10000 and 39999.
- Task 3
market_columns.py: the marketplace's columns, named like the website's
Write like_site(market) in market_columns.py. market is the marketplace's orders, as read_excel reads the Orders sheet of orders_marketplace.xlsx with sheet_name="Orders" and skiprows=2.
Give its columns the website's names: Order Id becomes order_id, Order Date becomes placed_at, SKU becomes sku, Qty becomes qty, Selling Price becomes price_rs, Ship City becomes city and Status becomes status; and take the Commission column out.
Return a DataFrame with exactly the columns order_id, placed_at, sku, qty, price_rs, city and status, in this order, with every value as it was (the price stays text: chapter 10 turns it into a number) and the same rows and labels.
The Check tries the marketplace's orders and a small table of 2 orders labelled 3 and 4.
- Task 4 · interview question
Interview: stock_fix.py, the line that changed nothing
Gopal's zero_lost(stock, skus) should set on_hand to 0 for the pieces the courier lost, but it prints a warning that starts with ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment, and the counts come back exactly as they were. Fix it. stock is one of Gopal's Sunday counts, as read_csv reads stock/stock_2026-09-27.csv (sku, on_hand, counted_by and note) or another week's (no note); skus is a list of skus.
Return a new DataFrame with the same columns, rows and labels as stock, where on_hand is 0 for every row whose sku is in skus and as it was everywhere else. zero_lost must not change the table it is given: the Check passes a table and then looks at it again.
The Check tries the count of 27 September with three skus, the count of 6 September with one, a sku that is not in the count (nothing changes), and a small count of 3 rows labelled 10 to 12.
Devika will ask: why did Gopal's line change nothing, and what does pandas 3's copy-on-write have to do with it?
What you will learn
- table["new"] = ...: a new column from other columns or from one value; the same line on an existing name replaces it.
- np.where, where and mask: a value for each row from a condition.
- pd.cut and pd.qcut: numbers into labelled bands, by edges or by equal counts.
- .loc[mask, "column"] = value: change only some rows.
- Copy-on-write: a table you made from another is its own copy, and chained assignment never works; .copy() when you mean a new table.
- assign, insert, rename and drop: new columns that return a new table, a column at a position, new names, and columns or rows taken out.
More in Selecting and cleaning
Build "New and changed columns" 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.