All lessons

Module 8: Analyses employers ask for · Lesson 8.1 · about 15 min

Readable pandas: method chaining and assign

Write pandas as one readable recipe: chain steps with dots, add columns with assign and lambda, filter with query, and use pipe for your own functions, so each analysis reads top to bottom.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 8 · about 15 minutes · uses Modules 2 to 4. You’ll be able to write an analysis as a single readable chain.

A chain writes the analysis as one recipe, top to bottom: each line takes the table from the line above, does one thing, and passes it on.

In everyday terms: a recipe card: “sift, add eggs, whisk, bake”. You do not name every bowl along the way; you follow the steps in order.

1. Many lines, many names

Step by step code makes a new name, or overwrites the same one, at every step:

Pythonorders.csv

It works, but you must read every line to know what df holds now.

2. The same as one recipe

Pythonorders.csv

Read it like a recipe: take the orders, keep 2026, add revenue, total per store, sort, round. The brackets let the chain spread over many lines, one step per line, so you can comment out a step to see the table before it.

3. pipe for your own steps

Pythonorders.csv

Small named functions plus pipe make a chain that a teammate can read and reuse.

( ... ) around the chain
Lets each step sit on its own line.
.assign(new=lambda d: ...)
Adds a column inside the chain.
.query('...')
Filters inside the chain.
.pipe(my_function)
Runs your own step as part of the chain.
Physical meaning
A chain does exactly what the separate lines did, in the same order; it just leaves no half-finished tables lying around to be reused by mistake. Comment out one line to see the table at that step.

“Chaining is faster.” It runs at the same speed. Its value is that it reads in order, with no temporary names like df2 and df_final that go stale.

Watch it happen, one step at a time:

Start.
storeqtyprice
S0125
S0218
S0145
S0234
.query('qty >= 2'). Drops the S02 row with qty 1: 3 rows left.
.assign(revenue=lambda d: d.qty * d.price). 10, 20, 12.
.groupby('store')['revenue'].sum().
storerevenue
S0130
S0212
.sort_values(ascending=False). S01 first. Four steps, one readable block.

4. Your turn

Your turnorders.csv
How it works

Principle: every method returns a new table, so a.f().g() means “do f, then do g to the result”. That is function composition, the same idea as g(f(a)) in maths, written left to right so it reads in the order things happen.

No in-place changes: assign leaves the original alone, so you can rerun a cell without breaking it. Section 1 writes a column into a filtered slice: older pandas warned about that (“SettingWithCopyWarning”), and pandas 3 quietly makes a copy. A chain never has the question at all.

Debugging a chain: put .pipe(lambda d: print(d.shape) or d) between steps to print the size at that point.

5. Practice

  1. Rewrite df = df[df.qty > 2]; df = df.sort_values('qty') as a chain.
  2. Why does assign(x=orders['qty'] * 2) go wrong after a query in the same chain?
  3. Write a function clean(d) and use it with pipe.
Answers
  1. df.query('qty > 2').sort_values('qty').
  2. It uses the full, unfiltered orders; pandas lines rows up by index, so it works by luck or fills NaN. Use lambda d: d['qty'] * 2.
  3. def clean(d): return d.dropna().drop_duplicates() then orders.pipe(clean).

New words: method chaining, assign, lambda, query, pipe, in place.

  • Wrap the chain in brackets, one step per line.
  • assign with lambda d: adds columns from the table at that step.
  • pipe runs your own function as a step.
  • Debug by commenting out lines from the bottom.

Check yourself

1What does assign() return?
2Inside a chain, why write assign(revenue=lambda d: d['qty'] * d['unit_price'])?
3What is pipe(f) for?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.