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.
Module 8 · about 15 minutes · uses Modules 2 to 4. You’ll be able to write an analysis as a single readable chain.
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:
It works, but you must read every line to know what df holds now.
2. The same as one recipe
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
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:
| store | qty | price |
|---|---|---|
| S01 | 2 | 5 |
| S02 | 1 | 8 |
| S01 | 4 | 5 |
| S02 | 3 | 4 |
| store | revenue |
|---|---|
| S01 | 30 |
| S02 | 12 |
4. Your turn
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
- Rewrite
df = df[df.qty > 2]; df = df.sort_values('qty')as a chain. - Why does
assign(x=orders['qty'] * 2)go wrong after aqueryin the same chain? - Write a function
clean(d)and use it withpipe.
Answers
df.query('qty > 2').sort_values('qty').- It uses the full, unfiltered
orders; pandas lines rows up by index, so it works by luck or fills NaN. Uselambda d: d['qty'] * 2. def clean(d): return d.dropna().drop_duplicates()thenorders.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
Sources
- Augspurger, T. (2016), Modern Pandas: Method Chaining https://tomaugspurger.net/posts/method-chaining/
- pandas documentation, User Guide: pandas.DataFrame.pipe https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pipe.html
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.