Pulse Data Hub

Open Data for Data Science: Four Checks on a Real File

Open Data for Data Science: Four Checks on a Real File

Open data for data science sounds like free fuel: download a CSV, run a model. In practice the first hour goes on finding out what the file really contains. This article runs four checks on a widely used open dataset, the World Bank population series, and each one turned up a real problem. All outputs below come from the code shown, run with pandas 3.0.5.

Bar chart: summing all 264 rows of the population file gives 85.6 billion people, against 7.99 billion in the World row and 7.58 billion across 139 country rows
The same 2022 population column, totalled three ways. Only the last two are sensible.

Check 1: what is a row?

The file comes from the datasets/population repository, which republishes World Bank population data. It looks like one row per country per year. It is not.

import pandas as pd
url = "https://raw.githubusercontent.com/datasets/population/main/data/population.csv"   # World Bank series
pop = pd.read_csv(url)
print(pop.shape, "rows;", pop["Country Name"].nunique(), "'countries'; years", pop.Year.min(), "-", pop.Year.max())

y = pop[pop.Year == 2022]
print(y.sort_values("Value", ascending=False).head(6)[["Country Name", "Value"]].to_string(index=False))
print("sum of all 2022 rows: %.1f bn people" % (y.Value.sum() / 1e9),
      "| the 'World' row says %.2f bn" % (y[y["Country Name"] == "World"].Value.iloc[0] / 1e9))
(17394, 4) rows; 264 'countries'; years 1960 - 2025
              Country Name      Value
                     World 7988550201
          IDA & IBRD total 6793703268
       Low & middle income 6590522389
             Middle income 5882746344
                 IBRD only 4928196300
Early-demographic dividend 3470644834
sum of all 2022 rows: 85.6 bn people | the 'World' row says 7.99 bn

The 264 “countries” include income groups, regions and the world itself. The biggest “country” is World, followed by groupings such as “Low & middle income”. A naive sum of the 2022 column gives 85.6 billion people, about 10.7 times the World row of 7.99 billion. Any “top 10 countries by population” chart built without filtering would be a list of aggregates. This is the most common way open data goes wrong: the file mixes levels, and nothing in the columns says which rows are which.

Check 2: filter to real countries

This file has no “is a country” flag. One workaround is to keep only rows whose ISO code appears in a country-level dataset, here the Gapminder file. It is a stand-in, not an official list, and it drops countries Gapminder does not cover.

# the file has no "is a country" flag; use the ISO codes of a country-level dataset as a filter
gap = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv")
countries = y[y["Country Code"].isin(set(gap.iso_alpha))]
print(len(countries), "country rows of", len(y), "| sum: %.2f bn" % (countries.Value.sum() / 1e9))
top = countries.sort_values("Value", ascending=False).head(5)
print((top.set_index("Country Name").Value / 1e6).round(0))

# growth 1960 -> 2022 for countries present in both years
a = pop[pop.Year == 1960].set_index("Country Code").Value
b = pop[pop.Year == 2022].set_index("Country Code").Value
g = (b / a).dropna()[lambda s: s.index.isin(set(gap.iso_alpha))]
names = pop.drop_duplicates("Country Code").set_index("Country Code")["Country Name"]
print(len(g), "countries | median multiple", round(g.median(), 2), "| shrank:", int((g < 1).sum()))
print(names[g.sort_values().index[:4]].tolist())
139 country rows of 264 | sum: 7.58 bn
Country Name
India            1425.0
China            1412.0
United States     334.0
Indonesia         279.0
Pakistan          244.0
Name: Value, dtype: float64
138 countries | median multiple 3.45 | shrank: 4
['Bulgaria', 'Croatia', 'Hungary', 'Bosnia and Herzegovina']

The filter leaves 139 country rows totalling 7.58 billion, which is 5% below the World row of 7.99 billion because small countries are missing, so this list answers “largest countries” but not “all people”. In 2022 the five largest were India (1,425 million), China (1,412), the United States (334), Indonesia (279) and Pakistan (244). Across the 138 countries with data in both 1960 and 2022, the median country’s population multiplied 3.45 times, and only four shrank: Bulgaria, Croatia, Hungary and Bosnia and Herzegovina.

Check 3: do two open sources agree?

Gapminder’s file also has population, for 2007. Comparing it with the World Bank value for the same year is a quick test of how far you can trust either.

# two "open" sources, same year: do they agree?
g7 = gap[(gap.year == 2007) & (gap.country != "Korea, Dem. Rep.")].set_index("iso_alpha")["pop"]
w7 = pop[pop.Year == 2007].set_index("Country Code").Value
m = pd.concat([g7, w7], axis=1, keys=["gapminder", "worldbank"]).dropna()
m["gap"] = (m.gapminder / m.worldbank - 1).abs()
print(len(m), "countries | median difference", round(100 * m.gap.median(), 1), "%",
      "| over 5%:", int((m.gap > .05).sum()), "| over 20%:", int((m.gap > .2).sum()))
print(m.sort_values("gap", ascending=False).head(3).round(2))
print("rows with ISO code KOR in 2007 before the fix:",
      int((gap[gap.year == 2007].iso_alpha == "KOR").sum()))
139 countries | median difference 4.2 % | over 5%: 61 | over 20%: 11
     gapminder  worldbank   gap
ERI   4906585.0  2743222.0  0.79
GNQ    551201.0   1036044.0  0.47
DJI    496374.0    874528.0  0.43
rows with ISO code KOR in 2007 before the fix: 2

Two respected open sources disagree by a median of 4.2% on 2007 population, by more than 5% for 61 of 139 countries and by more than 20% for 11. Eritrea is the extreme: 4.9 million in one file, 2.7 million in the other. Population estimates are revised as censuses arrive and the two files were built at different times, so neither is simply wrong, but you should never treat one as ground truth. The last line shows a different kind of fault: in the Gapminder file both Korea, Rep. and Korea, Dem. Rep. carry the ISO code KOR in 2007. Joining on that code would silently attach North Korea’s 23.3 million to South Korea’s 49.0 million, which is why the check above drops the second row first.

Check 4: record what you did

None of these problems is visible in a file preview, and none produces an error. The habit that protects you is small: print the shape and the first rows, look for aggregate or duplicate keys, compare one number against a second source, and write down the filter you used so someone can reproduce it. Our guides to analysing a real-world dataset and cleaning data with Python go deeper on these steps, free data science resources lists where to find datasets, and pandas data manipulation covers the joins and filters used here. We did not check licences for every file; confirm the licence of any dataset before you redistribute it.

Frequently asked questions

What is open data?

Open data is data that anyone may use and share, usually under a published licence. Governments, international bodies such as the World Bank, and research groups publish it. Open does not mean clean or consistent.

Where can I find open data for data science projects?

Start with the World Bank data portal, national government portals, and GitHub repositories that republish datasets, such as the one used here. Always read the documentation for what each row and column means.

How do I check an open dataset before using it?

Check what a row represents, look for duplicate or aggregate keys, compare a few values against another source, and note any filter you apply. These four steps caught every issue in this article.

Newsletter Signup Form

Latest tutorials

Latest articles

On this page
Verified by MonsterInsights