Data Science for Social Good: Life Expectancy, Tested
Data science for social good starts with a plain question: where is human wellbeing improving, where is it not, and what does the data say about why? This article uses the Gapminder dataset of life expectancy and income for 142 countries between 1952 and 2007. It finds large gains, two countries that went backwards, and a clear warning about relying on income as the explanation. Every number is output from the code shown, run with pandas 3.0.5 and NumPy 2.5.3.

How much did life expectancy improve?
The data is the Gapminder country-year table (1,704 rows, one every five years), distributed in the Plotly datasets repository. Life expectancy is in years; income is GDP per head in international dollars as documented by Gapminder, which this article does not re-verify.
import pandas as pd, numpy as np
url = "https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv"
g = pd.read_csv(url)
print(g.shape, g.country.nunique(), "countries, years", g.year.min(), "-", g.year.max(), "(every 5 years)")
a = g[g.year == 1952].set_index("country"); b = g[g.year == 2007].set_index("country")
gain = (b.lifeExp - a.lifeExp).dropna()
print("median life expectancy:", round(a.lifeExp.median(), 1), "->", round(b.lifeExp.median(), 1))
print("people-weighted world average:",
round(np.average(a.lifeExp, weights=a["pop"]), 1), "->",
round(np.average(b.lifeExp, weights=b["pop"]), 1))
print("fell:", gain[gain < 0].round(1).to_dict())
print("smallest gains:", gain.sort_values().head(5).round(1).to_dict())
print("median gain by continent:", gain.groupby(b.continent).median().round(1).to_dict())
(1704, 8) 142 countries, years 1952 - 2007 (every 5 years)
median life expectancy: 45.1 -> 71.9
people-weighted world average: 48.9 -> 68.9
fell: {'Swaziland': -1.8, 'Zimbabwe': -5.0}
smallest gains: {'Zimbabwe': -5.0, 'Swaziland': -1.8, 'Zambia': 0.3, 'Lesotho': 0.5, 'Botswana': 3.1}
median gain by continent: {'Africa': 16.0, 'Americas': 21.6, 'Asia': 24.7, 'Europe': 12.6, 'Oceania': 11.5}Median life expectancy rose by 26.8 years in 55 years, from 45.1 to 71.9. Weighting by population gives a smaller picture (48.9 to 68.9, a gain of 20.0 years) because India and China dominate the weights and started from a low level. Two countries lost ground: Zimbabwe (−5.0 years) and Swaziland (−1.8). Zambia, Lesotho and Botswana barely moved. Asia had the largest median gain at 24.7 years, Africa 16.0; Europe’s 12.6 reflects a high starting point. Gapminder’s data stops in 2007, so none of this describes the years since.
Does income explain the differences?
Richer countries live longer, but the relationship bends: each doubling of income buys fewer years than the last. The code fits life expectancy against the logarithm of income for 2007, then lists countries far above and below that line.
s = g[g.year == 2007].copy()
x, y = np.log(s.gdpPercap), s.lifeExp
slope, icpt = np.polyfit(x, y, 1)
s["gap"] = y - (slope * x + icpt) # years above/below what income predicts
print("R2", round(np.corrcoef(x, y)[0, 1] ** 2, 3), "| +", round(slope * np.log(2), 1), "years per doubling of income")
cols = ["country", "gdpPercap", "lifeExp", "gap"]
print(s.nlargest(5, "gap")[cols].round(1).to_string(index=False))
print(s.nsmallest(5, "gap")[cols].round(1).to_string(index=False))
mid = s[s.gdpPercap.between(3000, 6000)]
print(len(mid), "countries with $3,000-6,000 per head: life expectancy", round(mid.lifeExp.min(), 1),
"(", mid.loc[mid.lifeExp.idxmin(), "country"], ") to", round(mid.lifeExp.max(), 1),
"(", mid.loc[mid.lifeExp.idxmax(), "country"], ")")
R2 0.654 | + 5.0 years per doubling of income
country gdpPercap lifeExp gap
Vietnam 2441.6 74.2 13.1
Nicaragua 2749.3 72.9 10.9
West Bank and Gaza 3025.3 73.4 10.7
Comoros 986.1 65.2 10.5
Korea, Dem. Rep. 1593.1 67.3 9.2
country gdpPercap lifeExp gap
Swaziland 4513.5 39.6 -25.9
Angola 4797.2 42.7 -23.3
Botswana 12569.9 50.7 -22.2
South Africa 9269.7 49.3 -21.4
Equatorial Guinea 12154.1 51.6 -21.1
21 countries with $3,000-6,000 per head: life expectancy 39.6 ( Swaziland ) to 76.4 ( Albania )
1952 fitted life expectancy at $1,000: 43.1 | at $10,000: 63.5
2007 fitted life expectancy at $1,000: 54.7 | at $10,000: 71.3
12 countries had lower income per head in 2007 than 1952; median life-expectancy gain 19.7 yearsIncome per head explains about 65% of the variation in 2007 life expectancy (R² = 0.654), worth roughly 5.0 years per doubling. The rest is large. Among 21 countries with 3,000 to 6,000 dollars per head, life expectancy runs from 39.6 years in Swaziland to 76.4 in Albania, a 36.8-year spread at similar income. Vietnam, Nicaragua, the West Bank and Gaza and Comoros sit 10 to 13 years above the line. Swaziland, Angola, Botswana, South Africa and Equatorial Guinea sit 21 to 26 years below it. The data cannot say why. The HIV/AIDS epidemic is the usual explanation for several southern African countries, and oil-driven incomes for Angola and Equatorial Guinea, but neither was tested here.
Is it just economic growth?
# was the progress just economic growth? compare the income-health curve in 1952 and 2007
for yr in (1952, 2007):
q = g[g.year == yr]; k, c = np.polyfit(np.log(q.gdpPercap), q.lifeExp, 1)
print(yr, "fitted life expectancy at $1,000:", round(k * np.log(1000) + c, 1),
"| at $10,000:", round(k * np.log(10000) + c, 1))
j = pd.concat([a.gdpPercap, b.gdpPercap, gain], axis=1, keys=["i52", "i07", "gain"]).dropna()
poorer = j[j.i07 < j.i52]
print(len(poorer), "countries had lower income per head in 2007 than 1952; median life-expectancy gain",
round(poorer.gain.median(), 1), "years")Not entirely. A country with 1,000 dollars per head was predicted to reach 43.1 years in 1952 but 54.7 in 2007, a shift of 11.6 years at the same income; at 10,000 dollars the shift was 63.5 to 71.3. And 12 countries ended with lower income per head than in 1952 (including Haiti, Madagascar and the Democratic Republic of the Congo) yet gained a median 19.7 years. Whatever drove that, it was not income growth. This is the sort of finding where a social-good data project earns its keep: it separates where money matters from where other factors dominate, so effort goes where it can help. It also shows the limits of country averages, which hide differences within each country.
Using this in your own social-impact work
Treat national averages as a starting map, not a verdict. Check units and definitions in the source, compare weighted and unweighted views as above, and look for outliers before trusting a trend line. For more practice, see data literacy, exploratory data analysis in Python, interactive visualisations with Plotly (the same dataset ships with Plotly) and our climate change analysis. Limits of this article: one dataset, data ending in 2007, no causal modelling, and only the countries Gapminder includes.
Frequently asked questions
What does data science for social good mean?
It means applying data analysis to public-interest questions such as health, education, poverty or the environment, usually with open data and with the aim of informing decisions rather than selling a product.
Why do countries with the same income have different life expectancy?
Income is only one factor. In this dataset the range among 21 similar-income countries was 36.8 years, so health systems, disease burden, conflict and other factors clearly matter, though this analysis cannot separate them.
Where can I find data for social good projects?
Gapminder, the World Bank and national statistics offices publish free data. Read each source’s notes on units and revisions before drawing conclusions.
