Scripting for analysts: reading Python and notebooks
Learn to read a short Python script and a notebook the way an analyst needs to: follow the lines, question what they count, spot hidden state, and check a number by a second route.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: A recipe card. A recipe card lists the steps in the right order. Take out the flour. Add two eggs. Stir. Bake. You read it from the top to the bottom, and each line does one small thing. If you can read the card, you can tell whether the cake will come out right, even if you cannot bake it yourself.
A Python script is a recipe for a computer. Python is a popular programming language, a way to give a computer steps in writing. A is a recipe card with room for notes. It mixes your notes, small pieces of code and the results of running them, one piece at a time.
Sam is the , or (BA), for Daily List, a to-do app. Leo, the developer, shares a notebook with four lines. It loads the task export, groups the tasks by status and counts each group. Nina, the product owner, asks, "Can we trust the number?" Sam does not need to write the notebook. He needs to read it.
Two more ideas help. A DataFrame is a table of rows and columns held in memory, which the script can filter, group and count. is a trap: values kept in memory from cells that ran earlier or in another order.
The picture has a limit. A recipe is the same every time you read it. A notebook can give a different result if its pieces were run in a different order.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB The picture helps a newcomer remember to read a script line by line, ask what each step keeps or drops, and test it from a clean start.
- What comes in?
- What is kept or dropped?
- What is grouped and counted?
- Run it fresh from the top
- Check the number a second way
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| The recipe card | A script or notebook |
| One line of the recipe | One step on the table |
| The mixing bowl | The table in memory |
| A bowl with last night's leftovers | Hidden notebook state |
| Washing the bowl and cooking again from step one | Restart and run all |
WHERE THIS PICTURE STOPS
- A recipe reads the same every time, but a notebook can give a different result if pieces ran in a different order.
- A cook can taste as they go, but a number does not tell you if it is wrong.
- A recipe has one cook, but a notebook is often edited by several people.
HOOK
Read down, count what is kept, run it fresh.
REMEMBER IT THIS WAY
A chunk: a long list grouped so it is easier to hold.
Read the notebook from the top: load, group, count. Ask what each line does.
| In the trick | What it stands for |
|---|---|
| load | loads the task export |
| group | groups the tasks by status |
| count | counts each group |
TELL A FRIEND
A notebook is a recipe card for data. Read it from the top, ask what comes in, what is kept and what is counted, and test it by restarting and running all from the start.
OFTEN MIXED UP WITH SQL for BAs: SELECT and WHERE. That topic shows how to ask a database for rows and counts. This one shows the same ideas written as Python lines in a notebook.
Sources & standards — 3 sources
- Implementation-specific
Group by: split-apply-combine (pandas user guide) ↗ — pandas project · GroupBy dropna section and examples: missing keys excluded by default; selected user-guide section read 7 November 2026, case executed with pandas 2.2.3.
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specific
Comparison with SQL (pandas documentation) ↗ — pandas project · GROUP BY count versus size and missing values; selected comparison sections read 7 November 2026, missing-key/id counterexample executed with pandas 2.2.3 and SQLite.
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specificnbformat4
The Jupyter Notebook format ↗ — Project Jupyter · Notebook structure; cell types; code cells, execution_count and outputs; nbformat 4 selected sections read 7 November 2026.
Selected primary-source sections read for the BA Analytics audit. This record does not certify unrelated chapters or product features. No reuse permission for publisher images is inferred.
PRACTISE AND KEEP
Sources for this topic — 10 sources
- Market practice
Group by: split-apply-combine (pandas user guide) ↗ — pandas project · Group by: split-apply-combine (pandas user guide): grouping a table by a column and counting each group; count skips missing values (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Comparison with SQL (pandas documentation) ↗ — pandas project · Comparison with SQL (pandas documentation): pandas steps shown beside the matching SQL; size is the closer match to a SQL row count, while count skips missing values (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
The Jupyter Notebook format (IPython documentation) ↗ — Project Jupyter · The Jupyter Notebook format (IPython documentation): a notebook is a sequence of cells; code cells hold source and outputs; the file is saved as JSON (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Fine-grained lineage for safer notebook interactions (research paper on arXiv) ↗ — arXiv · Fine-grained lineage for safer notebook interactions (research paper on arXiv): hidden state: a cell depends on code from cells that ran in a different order (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Data Structures (Python tutorial) ↗ — Python · Data Structures (Python tutorial): lists, dictionaries and ways to loop over them (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
More Control Flow Tools (Python tutorial) ↗ — Python Software Foundation · More Control Flow Tools (Python tutorial): the for statement loops over the items of a sequence in order (registry source; search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List, Sam, Nina, Leo, Tom, Mei and Lily are invented, and the tiny script and its counts are made up for teaching. Python and pandas have far more features than shown. The pages were search excerpts, and none was opened. The pandas pages describe their own versions, so check the version a team uses.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
- Implementation-specific
Group by: split-apply-combine (pandas user guide) ↗ — pandas project · GroupBy dropna section and examples: missing keys excluded by default; selected user-guide section read 7 November 2026, case executed with pandas 2.2.3.
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specific
Comparison with SQL (pandas documentation) ↗ — pandas project · GROUP BY count versus size and missing values; selected comparison sections read 7 November 2026, missing-key/id counterexample executed with pandas 2.2.3 and SQLite.
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specificnbformat4
The Jupyter Notebook format ↗ — Project Jupyter · Notebook structure; cell types; code cells, execution_count and outputs; nbformat 4 selected sections read 7 November 2026.
Selected primary-source sections read for the BA Analytics audit. This record does not certify unrelated chapters or product features. No reuse permission for publisher images is inferred.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.