How to Compare Demographic Trends Across Multiple African Countries Using World Bank Data
Comparing demographic trends across African countries is much more reliable when you use a consistent dataset and repeatable workflow.
The World Bank’s World Development Indicators (WDI) provides standardized demographic variables that can be accessed directly in Google Colab, allowing you to build reproducible comparisons across countries like Kenya, Nigeria, Ethiopia, and South Africa.
This guide shows a practical workflow using Python in Colab.
1. Setup: Load World Bank Data in Google Colab
We will use the wbgapi library, which is the cleanest way to access World Bank indicators.
!pip install wbgapi pandas matplotlib seaborn -q
import wbgapi as wb
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
2. Select African countries for comparison
We define a focused comparison set:
countries = ["KEN", "NGA", "ETH", "ZAF", "EGY"] # Kenya, Nigeria, Ethiopia, South Africa, Egypt
These represent different demographic stages across Africa.
3. Choose key demographic indicators
We will pull core demographic variables:
| Indicator | World Bank Code |
|---|---|
| Total population | SP.POP.TOTL |
| Population growth rate | SP.POP.GROW |
| Fertility rate | SP.DYN.TFRT.IN |
| Urban population % | SP.URB.TOTL.IN.ZS |
| Life expectancy | SP.DYN.LE00.IN |
4. Load data from World Bank API
Example: Fertility rate
fertility = wb.data.DataFrame(
'SP.DYN.TFRT.IN',
economy=countries,
time=range(2000, 2024),
columns='time'
)
Transpose for time-series structure:
fertility = fertility.T
fertility.index = fertility.index.astype(int)
fertility.head()
5. Visualize fertility trends across countries
plt.figure(figsize=(10,6))
for country in fertility.columns:
plt.plot(fertility.index, fertility[country], label=country)
plt.title("Fertility Rate Trends Across African Countries")
plt.xlabel("Year")
plt.ylabel("Births per Woman")
plt.legend()
plt.show()
What this reveals:
Nigeria and Ethiopia show higher fertility persistence
Kenya shows faster fertility decline
South Africa is already near replacement-level fertility
6. Compare population growth rates
pop_growth = wb.data.DataFrame('SP.POP.GROW',economy=countries,time=range(2000, 2024),columns='time').Tpop_growth.index = pop_growth.index.str.replace('YR', '').astype(int)pop_growth.plot(figsize=(10,6))plt.title("Population Growth Rate Across African Countries")plt.xlabel("Year")plt.ylabel("Annual % Growth")plt.show()
7. Urbanization comparison (structural transformation)
urban = wb.data.DataFrame('SP.URB.TOTL.IN.ZS',economy=countries,time=range(2000, 2024),columns='time').Turban.index = urban.index.str.replace('YR', '').astype(int)urban.plot(figsize=(10,6))plt.title("Urban Population Share Across African Countries")plt.xlabel("Year")plt.ylabel("% Urban Population")plt.show()
Interpretation:
Urbanization rises faster in Egypt and South Africa than in Ethiopia or Nigeria, signaling different economic transformation speeds.
8. Build a comparative “latest year snapshot”
Instead of time series, compare the most recent year:
latest = 2023
snapshot = pd.DataFrame({
"Fertility": fertility.loc[latest],
"Urbanization": urban.loc[latest],
"Pop Growth": pop_growth.loc[latest]
})
snapshot
9. Visual comparison using heatmap
plt.figure(figsize=(8,5))
sns.heatmap(snapshot, annot=True, cmap="YlGnBu")
plt.title("Demographic Comparison of African Countries (Latest Year)")
plt.show()
10. Normalize trends for better comparison (indexing)
To compare trajectories, not levels:
fertility_index = fertility.div(fertility.iloc[0]).mul(100)
fertility_index.plot(figsize=(10,6))
plt.title("Indexed Fertility Decline (Base Year = 2000)")
plt.ylabel("Index (2000 = 100)")
plt.show()
Why this matters:
Removes structural level bias
Highlights speed of demographic transition
Makes countries directly comparable
11. Interpret results using demographic transition logic
From World Bank data patterns:
Nigeria, Ethiopia → early-to-mid transition (high fertility, fast population growth)
Kenya → faster fertility decline (accelerating transition)
South Africa, Egypt → later transition stage (lower fertility, higher urbanization)
This is more meaningful than comparing raw population sizes.
Using Google Colab and World Bank data, demographic comparison becomes a structured analytical workflow rather than a descriptive exercise.
The key value is not in plotting data—but in identifying:
Speed of fertility decline
Urbanization trajectories
Population momentum differences
Transition stages across countries
This framework is directly applicable to forecasting labor supply, education demand, and long-term economic structure across Africa.
Advance Your Career With 16 Python Projects in Data & ML — All for $288.
Comments
Post a Comment