Sharing your work · lesson 4 of 8
Parts in this lesson
Apython -m pip freeze > requirements.txt1.4: the exact versions, saved
Bnp.random.default_rng(2024)the same random numbers on every run
CPath("data/raw") / "books.csv"a path that works on any computer

Reproducibility

Last updated

An analysis is reproducible when someone else, or you next year, can rerun it on another computer and get the same results. Most analyses aren't, for a handful of ordinary reasons. In this lesson you'll find each one in a project that only works on its author's laptop, and fix it: paths, packages, seeds, raw data, and run order, plus a README that says how to run it all.

By the end of this lesson you can

  • Explain what makes an analysis rerun the same anywhere
  • Pin packages, set seeds and use paths inside the project
  • Keep raw data unchanged and write a README that says how to run the project

Wrong way: works on my laptop

Ana built the 2024 review on her laptop. In March she went on leave, and Ben had to rerun it with the March data. He copied her folder, installed Python, and ran it. It failed, then failed differently, then ran and gave different numbers. Apply each fix and rerun:

Rerun on a fresh computer5 problems
Fixes

$ python -m pip install -r requirements.txt
$ jupyter nbconvert --to html --execute analysis.ipynb

  • ✗ FileNotFoundError: C:\Users\Ana\Downloads\books (2).csv
  • ✗ ModuleNotFoundError: No module named ’openpyxl’
  • ✗ bootstrap interval: 8.4 to 23.1 (the laptop said 9.2 to 23.7)
  • ✗ 2024 takings: ₱3,403,987 (the laptop’s copy had a typo fixed by hand in Excel)
  • ✗ NameError: name ’real’ is not defined (cell 7 was run before cell 5 on the laptop)

None of these is a bug in the analysis. Each is something on Ana's laptop that wasn't in the project: a file in Downloads, a package she'd installed months ago, a random draw, a fix made by hand, and the order she happened to run cells in.

The check: before calling an analysis finished, rerun it the way a stranger would: a new folder, a new environment from requirements.txt, and every notebook executed from the top.

A fresh rerun
git clone https://example.org/tindahan.git
cd tindahan
py -m venv .venv
.venv\Scripts\activate
(.venv) $ python -m pip install -r requirements.txt
(.venv) $ jupyter nbconvert --to html --execute analysis.ipynb

Seeds and paths

Bootstraps, shuffle tests and samples use random numbers, so each run gives slightly different results. A seed fixes the starting point, so the "random" numbers are the same every run. Run the cell.

analysis.ipynb

The same seed gives the same interval, ₱45,578 to ₱48,230, every time. A different seed gives a slightly different one, which is a useful check too: if changing the seed changes your conclusion, the result was luck.

The same cell reads its data with pathlib: Path("data/raw") / "books.csv" builds a path from parts with /, and works on Windows, macOS and Linux. The path is relative: it starts from the project folder, not from C:\Users\Ana. Open notebooks from the project folder (1.5), and every path works for anyone who has the folder.

Pin the packages too. python -m pip freeze > requirements.txt (1.4) saves the exact version of every package; pip install -r requirements.txt gets the same ones on another computer. pandas 2 and pandas 3 can give different results for the same code.

Raw data, read-only

Ana's copy of books.csv had one number fixed by hand in Excel. Ben's copy, straight from the owners, didn't. Nobody could tell which was right.

The rule: raw data is never edited. Keep it in data/raw/ exactly as it arrived (5.1), and make the files read-only (on Windows, right-click → Properties → Read-only). Every fix happens in code, in a cleaning script (5.10), which writes to data/clean/. Then each fix is written down, can be checked, and is applied again automatically when next month's raw file arrives.

The same goes for outputs: charts and reports are made by the notebooks. Delete them and rerun, and you should get them back exactly.

A README

A README is the first file anyone opens: what the project is, and how to run it, step by step. Here's the Tindahan one, as README.md in the project folder:

# Tindahan analysis

What it is: the 2024 business review for the four Tindahan stores.

## Run it

1. Python 3.14. Make an environment and install the packages:
   py -m venv .venv
   .venv\Scripts\activate          (macOS or Linux: source .venv/bin/activate)
   python -m pip install -r requirements.txt
2. Put the owners' export in data/raw/ (not in this repository: see Data).
3. python clean.py              writes data/clean/
4. jupyter nbconvert --to html --execute analysis.ipynb

## Data

data/raw/books.csv: monthly books, from the owners, January 2022 to December 2024.
Never edited by hand. All cleaning is in clean.py.

## Folders

data/raw/     as received, read-only
data/clean/   made by clean.py
reports/      made by the notebooks

Write it for Ben: someone who knows Python but not this project. Then test it the only way that works: follow it yourself, word for word, in a fresh folder.

Check what you learned

A bootstrap interval changes slightly every time the notebook runs. What fixes it?
One value in the raw file is wrong. Where should you fix it?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis