Module 1: Python for Data
Python Setup and Notebooks
Before you can analyse anything, you need a working environment that you trust. A clean setup saves you hours of confusing errors later, and a notebook gives you a place where code, results and notes live side by side. In this lesson you will install the tools used throughout this course, open your first notebook and learn the habits that keep notebook work reproducible.
Choosing an installation route
You have three sensible options. Pick one and stick with it for the course.
- Anaconda or Miniconda. Anaconda is a free distribution that bundles Python with pandas, NumPy, Matplotlib, Seaborn and Jupyter already installed. It is the easiest route on Windows and macOS. Miniconda is the lightweight version: you install only what you need.
- Standard Python plus pip. Install Python from python.org, create a virtual environment and install packages with
pip. This is common in professional teams and works well on Linux. - Google Colab. A browser-based notebook that needs only a Google account. Nothing to install, which is useful on a shared or low-powered computer, but your files live in the cloud session and are cleared when it ends unless you save them to Google Drive.
If you use pip, create an isolated environment so that this course does not clash with other projects:
python -m venv analysis-env
# Windows
analysis-env\Scripts\activate
# macOS / Linux
source analysis-env/bin/activate
pip install pandas numpy matplotlib seaborn openpyxl jupyterlab
jupyter lab
The last command opens JupyterLab in your browser. With Anaconda you can instead open Anaconda Navigator and click JupyterLab, or run jupyter lab from the Anaconda Prompt.
How a notebook works
A notebook is a document made of cells. A code cell holds Python; a Markdown cell holds headings and notes. You run a cell with Shift + Enter, and the result appears directly underneath. Behind the notebook sits a kernel, a running Python process that remembers every variable you have created.
That memory is both the power and the danger of notebooks. If you run cell 5, then edit and rerun cell 2, the kernel now holds a mix of old and new values. The notebook on screen no longer matches what Python knows. Two habits protect you:
- Run cells from top to bottom in order.
- Before you share or trust a result, use Kernel, Restart Kernel and Run All Cells. If everything still works, your notebook is reproducible.
Worked example: your first analysis notebook
Create a new notebook called 01_setup_check.ipynb. In the first cell, import the libraries and print their versions. Recording versions matters because pandas behaviour changes slightly between releases, and a colleague may need to match your setup.
import sys
import pandas as pd
import numpy as np
import matplotlib
print("Python:", sys.version.split()[0])
print("pandas:", pd.__version__)
print("NumPy:", np.__version__)
print("Matplotlib:", matplotlib.__version__)
In the next cell, build a tiny dataset of hypothetical daily sales for a Nairobi kiosk and look at it:
sales = pd.DataFrame({
"day": ["Mon", "Tue", "Wed", "Thu", "Fri"],
"revenue_kes": [4200, 3900, 5100, 4800, 6300],
})
sales["revenue_kes"].mean()
Notice that a notebook automatically displays the value of the last line in a cell, so you do not always need print. Add a Markdown cell above the code with a heading such as Kiosk sales check and one sentence explaining what the notebook does. Good notebooks read like short reports.
Organising your project folder
Keep each analysis in its own folder with a simple structure:
data/raw/for original files, which you never edit by handdata/processed/for cleaned outputs your code createsnotebooks/for your notebooks, numbered in the order they runoutputs/for charts and reports
Use relative paths such as "../data/raw/sales.csv" rather than full paths like C:\Users\Wanjiku\Desktop\sales.csv. Relative paths keep working when you move the folder or share it.
Common mistakes to avoid
- Installing packages into the wrong Python. If
import pandasfails after you installed it, your notebook kernel is probably using a different environment. Inside a notebook,%pip install pandasinstalls into the kernel you are actually running. - Running cells out of order and trusting the result. Restart and run all before you rely on anything.
- Editing raw data files in Excel before loading them. Do all changes in code so they can be repeated.
- Naming notebooks Untitled3.ipynb. Descriptive names save time when you return weeks later.
Once your setup check runs cleanly from a fresh kernel, you are ready for the rest of the course.
Exercise
Install your chosen environment and create a project folder with data/raw, data/processed, notebooks and outputs subfolders. Create a notebook named 01_setup_check.ipynb that prints the versions of Python, pandas, NumPy and Matplotlib. Add the kiosk sales DataFrame from the lesson and calculate the mean revenue. Finally, restart the kernel and run all cells to confirm the notebook works from top to bottom.
Key points
- A virtual environment or Anaconda keeps your course packages isolated and predictable.
- The kernel remembers variables, so cells run out of order can give misleading results.
- Restart and run all before trusting or sharing any notebook.
- Keep raw data untouched and use relative paths inside a clear folder structure.