Sub topic 4
A project layout that separates code, data, and outputs
This chapter assumes you can run Jupyter notebooks, use the command line, and make basic Git commits. It covers a folder layout that keeps inputs, logic, and results apart, so a notebook can be rerun by someone else.
Why layout matters
A notebook saved next to a pile of CSV files, plots, and scratch copies is hard to rerun. You can't tell which files are original inputs and which the notebook produced. Overwriting a raw file by accident can't be undone. A fixed layout answers three questions at a glance. What do I start from? What code runs? What does the code create?
A working layout
project/
├── README.md
├── requirements.txt
├── data/
│ ├── raw/ # original inputs, never edited
│ └── processed/ # files created by code
├── notebooks/
│ └── analysis.ipynb
├── src/
│ └── cleaning.py # reusable functions
└── outputs/
├── figures/
└── tables/Each folder has one job.
| Folder | Contents | Edited by hand? |
|---|---|---|
| `data/raw/` | Original downloads and exports | No |
| `data/processed/` | Cleaned or merged data | No, regenerate it |
| `notebooks/` | Exploration and narrative | Yes |
| `src/` | Functions shared across notebooks | Yes |
| `outputs/` | Figures, tables, executed notebooks | No, regenerate it |
The rule that matters most is that data/raw/ is read-only in practice. Code reads from it and never writes to it. Everything in processed/ and outputs/ can be deleted and rebuilt by running the code.
Using paths that work everywhere
A notebook that reads C:\Users\ana\Desktop\data.csv fails on any other machine. Use paths relative to the project instead. Jupyter starts a notebook with its own folder as the working directory, so notebooks/ is the starting point.
from pathlib import Path
PROJECT_ROOT = Path.cwd().parent
RAW = PROJECT_ROOT / "data" / "raw"
PROCESSED = PROJECT_ROOT / "data" / "processed"
FIGURES = PROJECT_ROOT / "outputs" / "figures"
PROCESSED.mkdir(parents=True, exist_ok=True)
FIGURES.mkdir(parents=True, exist_ok=True)
print("Reading from:", RAW)Define these paths once, in the first code cell. Every later cell then uses RAW / "measurements.csv" instead of a typed-out string.
What goes into Git
Commit code, the notebook, the README, and requirements.txt. Decide deliberately about data. Small raw files can be committed. Large ones belong in shared storage, with a note in the README saying where to get them. Generated folders are usually ignored:
data/processed/
outputs/
.venv/
.ipynb_checkpoints/Later chapters cover recording where raw data came from and checking it with a checksum.
Common errors
- Saving over raw data. Writing a cleaned table back to
data/raw/destroys the original. Always write toprocessed/. - Hand-edited processed files. A fix made in a spreadsheet can't be reproduced. Make the fix in code.
- Absolute paths. They break on every other machine.
- Outputs mixed with inputs. If figures sit in
data/, nobody knows what is safe to delete.
Exercise
A colleague's notebook writes clean.csv into data/raw/ next to the original measurements.csv. Give two problems this causes and say where the file should go.
Show answer
First, it mixes generated files with original inputs, so no one can tell which files are safe to delete or regenerate. Second, if the code ever reuses the same name as an original file, it overwrites data that can't be recovered. The file belongs in data/processed/, and the code should create that folder if it is missing.