Data analysis · Beginner · 16 min
Download a public energy dataset and get to know it before analysing
By Padding · Updated 18 Nov 2026
Python 3.15.2 · pandas 3.1.1 · uv 0.13 (checked on python:3.15-slim)
You will download the Our World in Data energy dataset at a pinned commit, prove it is the exact file with a SHA-256 check, read its codebook, answer the first questions about its rows, regions and gaps with pandas, and end with a notes.md of what to answer next.
On this page
Before you clean, chart or summarise a dataset, spend half an hour finding out what it is. How many rows? What does one row stand for? Which rows are countries and which are totals someone else added up? Which columns are mostly blank? Answers to those questions decide every later step, and writing them down stops you from rediscovering them in a week.
This is the first part of a series that asks one question: how has electricity generation per person, and the mix of sources behind it, changed across African countries since 2000? The data is the energy dataset published by Our World in Data (OWID) on GitHub. OWID's own work is released under CC BY 4.0. Data that OWID republishes from others keeps its source's licence, so the series sticks to columns from Ember, whose yearly electricity data is also CC BY 4.0, plus population. Credit both OWID and Ember whenever you share results.
You will work in ~/energy-demo. The later parts build on the files you create here, so keep the folder when you finish.
Step 1 · Create the project
Make the project folder, a raw/ folder for downloaded files, and add pandas. The version range accepts 3.1 patch releases only, so a later minor release cannot change results under you:
mkdir -p ~/energy-demo/raw
cd ~/energy-demo
uv init --no-package --python 3.15
uv add "pandas>=3.1,<3.2"Check it worked
uv run python -c "import pandas; print(pandas.__version__)"The command prints 3.1.1, or a later 3.1 release.
Step 2 · Download the files at a pinned commit
The dataset lives in the owid/energy-data repository. Its file at the master branch can change whenever OWID publishes an update, which would quietly change your numbers. Download from one fixed commit instead, so anyone who follows your work gets the same bytes. This tutorial uses commit f8de88a, from 1 October 2026.
The repository README now marks these files as the legacy release: OWID has moved to a new methodology and catalogue. A file that no longer changes is an advantage for a reproducible study. Note it, and check the new catalogue before you publish anything that needs the latest year.
Fetch the data and its codebook. --compressed asks the server for a gzip copy and unpacks it on arrival, so about 2.6 MB crosses the network instead of 9.2 MB. On prepaid mobile data that difference matters, and the file on disk is identical either way:
cd ~/energy-demo
BASE=https://raw.githubusercontent.com/owid/energy-data/f8de88a4f6f9cab44f1cf606047cbd6c4414042c
curl -fsSL --compressed -o raw/owid-energy-data.csv "$BASE/owid-energy-data.csv"
curl -fsSL --compressed -o raw/owid-energy-codebook.csv "$BASE/owid-energy-codebook.csv"
ls -l raw-f makes curl fail on an HTTP error instead of saving the error page as if it were your data.
Check it worked
ls -l lists two files: owid-energy-codebook.csv at 45793 bytes and owid-energy-data.csv at 9229369 bytes.
Step 3 · Prove the files are the ones you meant
A size match is a weak test. A SHA-256 checksum changes if a single byte changes, so it proves the file is exactly the one this tutorial was written against. Write a small checker with the expected values in it. It uses Python's hashlib, so it works the same on macOS, Linux and Windows:
cd ~/energy-demo
cat > check_raw.py <<'PY'
import hashlib
import sys
from pathlib import Path
EXPECTED = {
"owid-energy-data.csv": "266f2e2baad7975351bc9bb4aa061d22b1da9fe4c47d51d2ac6071e01e171f76",
"owid-energy-codebook.csv": "3cc9b7db0d921496e2988568ce3aee5ed41f50431dd234a0663b5f0a4b2e32bb",
}
all_ok = True
for name, expected in EXPECTED.items():
actual = hashlib.sha256(Path("raw", name).read_bytes()).hexdigest()
ok = actual == expected
all_ok = all_ok and ok
print("OK " if ok else "CHANGED ", name)
sys.exit(0 if all_ok else 1)
PY
uv run check_raw.pyThen make both files read-only. From here on, scripts read from raw/ and write their results somewhere else, so a slip in your code can never overwrite the original:
cd ~/energy-demo
chmod a-w raw/owid-energy-data.csv raw/owid-energy-codebook.csv
ls -l rawCheck it worked
check_raw.py prints OK next to both names and exits without an error. If you see CHANGED, delete the file, download it again and re-run; a second mismatch means the URL is wrong. The permissions column from ls -l now starts with -r--r--r-- for both files. Run uv run check_raw.py again at the start of every later part: it takes a second and catches a damaged copy before it reaches a chart.
Step 4 · Read the codebook
The codebook is a small CSV with one row per data column: its name, a title, a description, a unit and its source. Read it before the data. It tells you which columns are measured in terawatt-hours, which are kilowatt-hours per person and which are percentages, and where each number comes from.
Write a script that counts the columns by source and lists the ones that come from Ember alone:
cd ~/energy-demo
cat > codebook.py <<'PY'
import pandas as pd
book = pd.read_csv("raw/owid-energy-codebook.csv")
source = book["source"].fillna("")
ember = source.str.contains("Ember")
energy_institute = source.str.contains("Energy Institute")
print("columns described:", len(book))
print("Ember only:", int((ember & ~energy_institute).sum()))
print("Ember and Energy Institute:", int((ember & energy_institute).sum()))
print("no Ember:", int((~ember).sum()))
print()
print(book.loc[ember & ~energy_institute, ["column", "unit"]].to_string(index=False))
print()
key = ["population", "electricity_generation", "per_capita_electricity", "solar_share_elec"]
print(book.set_index("column").loc[key, ["unit"]].to_string())
PY
uv run codebook.pyThe counts tell an important story. Ten columns come from Ember alone, such as electricity_demand and carbon_intensity_elec. Another 38, including the ones this series needs most (electricity_generation, per_capita_electricity and the *_share_elec columns), combine Ember with the Energy Institute's Statistical Review of World Energy. The README changelog says OWID uses Ember's figures where both sources cover a country and year. Ember's coverage starts in 2000, so a question to settle in the next part is whether limiting the study to 2000 onwards keeps you on Ember's numbers. The remaining 82 columns, about primary energy, fossil fuel production and GDP, are out of scope.
Check it worked
The first four lines read columns described: 130, Ember only: 10, Ember and Energy Institute: 38 and no Ember: 82. The last table shows the units: people for population, terawatt-hours (TWh) for generation, kilowatt-hours (kWh) for generation per person, and % for the solar share.
Step 5 · Find out what one row means
Now load the data and measure it:
cd ~/energy-demo
cat > explore.py <<'PY'
import pandas as pd
df = pd.read_csv("raw/owid-energy-data.csv")
print("rows:", len(df))
print("columns:", df.shape[1])
print("years:", df["year"].min(), "to", df["year"].max())
print("distinct names:", df["country"].nunique())
print("repeated name-and-year pairs:", int(df.duplicated(["country", "year"]).sum()))
print()
nigeria = df[df["country"] == "Nigeria"]
cols = ["year", "iso_code", "population", "electricity_generation", "per_capita_electricity"]
print(nigeria[cols].tail(3).to_string(index=False))
PY
uv run explore.pyNo name appears twice for the same year, so each row is one place in one year. That is called long format: a country with 26 years of data has 26 rows. The columns hold measurements for that place and year, which is why a single row can have values in a few columns and blanks in the rest.
The Nigeria rows make the units concrete. In 2025 Nigeria generated 41.54 TWh for about 237.5 million people, which the dataset gives as roughly 175 kWh per person per year. Keep that scale in mind: a figure in the thousands for a West African country would be a sign you are reading the wrong column.
Check it worked
The first five lines read rows: 23377, columns: 130, years: 1900 to 2025, distinct names: 314 and repeated name-and-year pairs: 0. The table shows Nigeria (NGA) for 2023, 2024 and 2025, with per_capita_electricity between about 160 and 180.
Step 6 · Separate countries from aggregates
314 names is more than the number of countries in the world. The extra rows are aggregates: totals for regions or groups, added up by OWID or copied from a source. They carry no ISO country code. Add them to a country average and you count the same electricity twice.
List the names with no iso_code, the African ones in particular:
cd ~/energy-demo
cat > aggregates.py <<'PY'
import pandas as pd
df = pd.read_csv("raw/owid-energy-data.csv")
no_code = sorted(df.loc[df["iso_code"].isna(), "country"].unique())
print("names without an ISO code:", len(no_code))
print()
print("African aggregates:")
for name in no_code:
if "Africa" in name:
print(" ", name)
print()
print("no code and no source suffix:")
for name in no_code:
if "(" not in name:
print(" ", name)
ember_africa = df[df["country"] == "Africa (Ember)"]
print()
print("Africa (Ember) years:", ember_africa["year"].min(), "to", ember_africa["year"].max())
print("Africa (Ember) rows with population:", int(ember_africa["population"].notna().sum()))
PY
uv run aggregates.pyThe suffix in brackets names the source that drew the region's borders: (Ember), (EI) for the Energy Institute, (EIA) and (Shift). Names with no suffix, such as Africa and World, are OWID's own totals. So the file holds several different "Africa" totals, built from different country lists, and they will not agree with each other.
The second list holds a trap. It mixes real aggregates with places that have no current ISO code for other reasons: former countries such as USSR, Yugoslavia and Czechoslovakia, and Kosovo. "No ISO code" is a good first filter for aggregates, not a perfect one. Write that down.
Africa (Ember) has no population at all, so it has no per-person figures. To compare African subregions per person, you will need to build the regions yourself from country rows, which part 3 does.
Check it worked
The first line reads names without an ISO code: 94. The African list has 14 names, from Africa and Africa (EI) to Western Africa (EI), and does not include South Africa, which is a country with the code ZAF. The last two lines read Africa (Ember) years: 2000 to 2025 and Africa (Ember) rows with population: 0.
Step 7 · Find the mostly empty columns
A column that is 90 per cent blank across the whole file may still be complete for the years and places you care about. Measure both:
cd ~/energy-demo
cat > gaps.py <<'PY'
import pandas as pd
df = pd.read_csv("raw/owid-energy-data.csv")
blank = df.isna().mean()
print("columns more than half blank:", int((blank > 0.5).sum()), "of", len(blank))
recent = df[(df["year"] >= 2000) & df["iso_code"].notna()]
print("country rows from 2000:", len(recent), "for", recent["country"].nunique(), "countries")
cols = ["population", "electricity_generation", "per_capita_electricity",
"electricity_demand", "solar_share_elec", "coal_share_elec"]
print()
print("whole file, % blank")
print((blank[cols] * 100).round(1).to_string())
print()
print("countries from 2000, % blank")
print((recent[cols].isna().mean() * 100).round(1).to_string())
PY
uv run gaps.pyAcross the whole file, 113 of 130 columns are more than half blank, and per_capita_electricity is blank in about 70 per cent of rows. That looks alarming until you narrow the view. Electricity data mostly starts in 2000 (or 1985 for some countries), while population goes back to 1900, so most early rows have a population and nothing else. Restricted to countries from 2000 onwards, generation and generation per person are blank in only 2.9 per cent of rows.
Check it worked
The first two lines read columns more than half blank: 113 of 130 and country rows from 2000: 5549 for 220 countries. In the second table, electricity_generation and per_capita_electricity both show 2.9, and coal_share_elec shows 4.9.
Step 8 · Write down what you found and what to ask next
Notes are part of the analysis. Put the facts, the source and the open questions in notes.md at the top of the project. Later parts add their decisions to the same file:
cd ~/energy-demo
cat > notes.md <<'MD'
# African electricity since 2000: working notes
## Source
- Our World in Data energy dataset, owid-energy-data.csv and owid-energy-codebook.csv
- github.com/owid/energy-data at commit f8de88a4f6f9cab44f1cf606047cbd6c4414042c (1 Oct 2026),
legacy release, no longer updated. SHA-256 checked by check_raw.py. Files in raw/ are read-only.
- Licence: OWID data CC BY 4.0. Electricity columns from Ember (CC BY 4.0), some combined with the
Energy Institute Statistical Review. Credit: Our World in Data and Ember.
## What one row is
- One place in one year (long format). 23,377 rows, 130 columns, 1900 to 2025, 314 names.
- No repeated name-and-year pairs.
## Aggregates
- 94 names have no ISO code. 14 are African aggregates, e.g. Africa, Africa (Ember), Western Africa (EI).
- Not every no-code name is an aggregate: USSR, Yugoslavia, Czechoslovakia, East and West Germany,
Serbia and Montenegro, Kosovo.
- Africa (Ember) covers 2000 to 2025 and has no population, so no per-person values.
## Gaps
- 113 of 130 columns are more than half blank across the file.
- For countries from 2000: generation and generation per person 2.9% blank, coal share 4.9%.
## Questions to answer
1. Does limiting the study to 2000 onwards mean every electricity value comes from Ember?
2. Which African countries have too few years of generation data to compare?
3. Where should Kosovo and former countries go: drop, or keep with a note?
4. Which UN M49 subregion is each African country in?
5. Is 2025 complete for every African country, or should the study stop at 2024?
MD
grep -c "^[0-9]\." notes.mdCheck it worked
grep prints 5, the number of open questions. Open notes.md and confirm each number in it matches what your scripts printed. If OWID ever changes the file at this commit, check_raw.py fails first and tells you these notes no longer apply.
Next steps
- Answer the first three questions and save a tidy copy of the data: clean the energy data and keep only what you need, part 2 of this series.
- For a first look at text-heavy, messy CSV files, see clean a CSV file with pandas.
- Read the same data from Excel or Parquet: read Excel, CSV and Parquet files with pandas.
- Prefer to explore interactively: explore a dataset in JupyterLab.
Part of these paths
Related tutorials
- How to Clean and Summarise a CSV File with pandasYou will take a messy orders CSV with stray spaces, mixed spellings, money written as text, odd dates, missing values and a duplicate row, and clean it with pandas 3.1 on Python 3.15 into a tidy file plus per-country and per-month summaries.
- Read Excel, CSV and Parquet files into pandas correctlyYou will load a semicolon-separated CSV with the wrong encoding, a multi-sheet Excel workbook whose headers start on row 4 and a Parquet file into pandas 3.1 with the right types, leading zeros and dates intact, reading only the columns you need.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials