Lesson 39 · Real-World Data Analytics
Python Data Analytics #39: Global Health Spending vs Life Outcomes in Python
A real WHO-sourced dataset, twenty-six real countries tracked across sixteen real years, asking whether real health spending actually buys real longer lives.
- CourseReal-World Data Analytics
- Lesson39 of 100
- FormatJupyter notebook · 30 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- health_life_raw.csv preview46.8 KB
📓 Full notebook
Download .ipynbData Analytics 100, Video 39: Real Global Health Spending and Life Outcomes#
- Video thirty-nine of the hundred-video real-world data analytics series.
- A real WHO-sourced dataset, twenty-six real countries tracked across sixteen real years, asking whether real health spending actually buys real longer lives.
- Let's get into it.
This lesson is for learners who can group and filter a pandas table and want to practise a classic analyst task: testing whether one factor really explains an outcome. The question is a big one for governments and health funders: does spending more on health actually buy longer lives, or do other factors matter more?
The dataset, health_life_raw.csv, is WHO-sourced and has one row per country per year. The columns that matter most are Country, Year, Status (Developed or Developing), Life expectancy, Total expenditure (government health spending as a percentage of total government spending), GDP, Schooling (average years of schooling) and Income composition of resources (a 0 to 1 index of how well income is used for human development). It covers a limited set of countries, so the lesson scopes its claims carefully.
In this lesson you will:
- audit missing values and spot a reporting lag in the latest year
- fall back to an earlier year in a transparent way
- compare several correlations side by side to rank predictors
- measure per-country change with a self-merge
You should be comfortable with pandas groupby and .corr(); see earlier lessons in the series if not.
Part 1: A Real Health and Development Dataset, Honestly Scoped#
Before asking whether spending buys longer lives, you need to know how much data you are working with. The code imports pandas, numpy and matplotlib, reads the CSV and shows .shape. The output, (416, 22), means 416 rows and 22 columns. With 26 countries and 16 years, 26 times 16 is exactly 416, so it looks like one row per country per year. That is a small slice of the world, which is why the heading stresses honest scoping.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
health = pd.read_csv('health_life_raw.csv')
health.shape
Part 2: Real Data Preview#
With 22 columns, it helps to see every name before you write any code. The code calls health.head(5) and then health.columns.tolist(); only the last expression is displayed, so the output is the list of names. Notice the inconsistent styles: some are capitalised (Life expectancy), others lower case (infant deaths, percentage expenditure), and several contain spaces. You must type them exactly, including spaces, so copying from this list avoids frustrating KeyError messages.
health.head(5)
health.columns.tolist()
Part 3: Real Missing Data Audit#
Missing values decide which questions you can answer reliably. isna().sum() counts gaps per column. Life expectancy and Schooling are complete, but Total expenditure is missing 24 values, GDP 32, Hepatitis B 34 and Population 96. That matters directly here, because spending and GDP are two of the factors you want to test. Any correlation involving them will be based on fewer than 416 rows, so you will drop the missing rows explicitly before calculating.
health.isna().sum()
Part 4: Real Coverage Check#
Confirm the time span before you choose a latest year. The first line counts countries, but only the last expression is displayed, so the output is the year range: 2000 to 2015. Run the nunique() line on its own to see the country count, which the recap later confirms as 26. Knowing the final year is 2015 tells you which year to use for a recent snapshot.
health['Country'].nunique()
health['Year'].min(), health['Year'].max()
Part 5: Real Developed vs Developing Split#
Comparing developed and developing countries is a natural first split, but you need to know how balanced it is. value_counts() counts each Status value across all rows. The output shows 352 Developing rows and 64 Developed rows. Since each country has 16 years, that is 22 developing and 4 developed countries. Any developed-country average therefore rests on just four countries, so treat it as indicative rather than definitive.
health['Status'].value_counts()
Part 6: Real 2015 Life Expectancy by Status#
A single-year view avoids mixing old and new figures. The code filters for 2015 with .copy() to make an independent table, then groups by Status and takes the mean of Life expectancy, rounded to one decimal. Developed countries average 80.0 years and developing countries 71.7, a gap of about eight years. Keep in mind the developed group is only four countries, as the previous step showed.
latest = health[health['Year'] == 2015].copy()
latest.groupby('Status')['Life expectancy'].mean().round(1)
Part 7: Real Reporting Lag, Caught Honestly#
The latest year is not always the most complete, because some figures are reported a year or two late. latest['Total expenditure'].isna().sum() counts how many 2015 rows lack spending data. The output is 24. Since latest has one row for each of the 26 countries, almost every country is missing its 2015 spending figure. In fact, all 24 missing spending values found in the audit are in 2015. Catching this early stops you from building a 2015 comparison on two countries.
missing_2015_spending = latest['Total expenditure'].isna().sum()
missing_2015_spending
Part 8: Real Falling Back to 2014 Spending Data#
When the newest year is incomplete, a sensible fix is to use the most recent complete year and say so clearly. The code filters for 2014 and uses dropna(subset=['Total expenditure']) to drop any rows still missing spending, then .copy(). The output, 26, means every country has a 2014 spending value. Mixing 2015 outcomes with 2014 spending is a reasonable approximation, but always state it in your write-up.
spend_2014 = health[health['Year'] == 2014].dropna(subset=['Total expenditure']).copy()
len(spend_2014)
Part 9: Real Highest Health Spenders#
Ranking spending shows which countries put the largest share of their budgets into health. sort_values('Total expenditure', ascending=False).head(5) keeps the top five for 2014. Austria leads at 11.21%, followed by Bosnia and Herzegovina (9.57%), Australia (9.42%), Bulgaria (8.44%) and Brazil (8.32%). This measure is a share, not money per person, so a richer country can spend far more in absolute terms with a similar percentage.
top_spenders = spend_2014.sort_values('Total expenditure', ascending=False).head(5)[['Country', 'Total expenditure']]
top_spenders
Part 10: Real Lowest Health Spenders#
The bottom of the list is just as revealing, and sometimes surprising. Sorting with ascending=True and taking head(5) gives Belgium lowest at 1.59%, then Brunei Darussalam (2.65%), Bangladesh (2.82%), Angola (3.31%) and Bhutan (3.57%). Belgium being lowest is unexpected for a wealthy European country. When a result clashes this strongly with what you know, check it against another source before reporting it, as it may be a data quality issue.
bottom_spenders = spend_2014.sort_values('Total expenditure', ascending=True).head(5)[['Country', 'Total expenditure']]
bottom_spenders
Part 11: Real Spending Chart#
A bar chart of all 26 countries shows the full spread rather than just the extremes. The code uses set_index('Country') so country names become bar labels, selects Total expenditure, sorts it and draws horizontal bars with kind='barh'. The figure is saved to health_spending_by_country.png and closed, so nothing is shown inline. When you open it, look at where Belgium sits and whether most countries cluster in a narrow middle band.
fig, ax = plt.subplots(figsize=(10, 6))
spend_2014.set_index('Country')['Total expenditure'].sort_values().plot(kind='barh', ax=ax, color='steelblue')
ax.set_xlabel('Real Total Health Expenditure (% of GDP)')
ax.set_title('Real Health Spending as a Share of GDP, 2014')
plt.tight_layout()
plt.savefig('health_spending_by_country.png', dpi=100)
plt.close()
Part 12: Real Spending vs Life Expectancy Correlation#
This is the central question: do countries that spend a larger share on health live longer? The code first uses dropna on both columns so only complete pairs are compared, then .corr() computes the Pearson correlation, which runs from -1 to 1. The output is 0.108, a very weak positive relationship. On its own, spending share explains very little of the difference in life expectancy across these country-years.
spend_life = health.dropna(subset=['Total expenditure', 'Life expectancy'])
spend_corr = round(spend_life['Total expenditure'].corr(spend_life['Life expectancy']), 3)
spend_corr
Part 13: Real GDP vs Life Expectancy Correlation#
If spending share is weak, national wealth is the obvious next candidate. The same pattern, dropna on GDP and Life expectancy then .corr(), gives 0.473, a moderate positive correlation. Richer countries tend to live longer, more clearly than higher-spending ones. Remember GDP had 32 missing values, so this correlation is based on fewer rows than the full table.
gdp_life = health.dropna(subset=['GDP', 'Life expectancy'])
gdp_corr = round(gdp_life['GDP'].corr(gdp_life['Life expectancy']), 3)
gdp_corr
Part 14: Real Schooling vs Life Expectancy Correlation#
Education often matters for health through better nutrition, hygiene and use of services. Schooling has no missing values, so the code calls .corr() directly; pandas would skip missing pairs anyway. The output is 0.669, the strongest relationship so far. Countries with more years of schooling tend to have noticeably longer life expectancy, though that does not prove schooling is the cause.
schooling_corr = round(health['Schooling'].corr(health['Life expectancy']), 3)
schooling_corr
Part 15: Real Income Composition vs Life Expectancy#
The income composition of resources is a 0 to 1 index from the UN's Human Development Index, measuring how well a country's income turns into human development. .corr() with life expectancy gives 0.612, a strong positive relationship, close to schooling. Since this index partly reflects education and income, it is not independent of the earlier measures, which is worth remembering when comparing them.
income_corr = round(health['Income composition of resources'].corr(health['Life expectancy']), 3)
income_corr
Part 16: Real Predictor Comparison Table#
Putting the four correlations side by side makes the ranking easy to see and to report. The code builds a pd.Series from a dictionary of names and values, then sorts from highest to lowest. Schooling tops the list at 0.669, then income composition at 0.612, GDP at 0.473, and health spending share last at 0.108. Building a small summary Series like this is a tidy habit whenever you compare several results.
predictors = pd.Series({'Total expenditure': spend_corr, 'GDP': gdp_corr, 'Schooling': schooling_corr, 'Income composition': income_corr}).sort_values(ascending=False)
predictors
Part 17: Real Schooling Scatter Chart#
A scatter chart lets you check whether the correlation is driven by a genuine pattern or by a few odd points. ax.scatter plots each country-year as a dot, with schooling on the x-axis and life expectancy on the y-axis, and alpha=0.5 makes dots semi-transparent so overlapping points show up darker. The figure is saved to schooling_vs_life_expectancy.png and closed. Look for an upward-sloping cloud, and for any points far from the main group.
fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(health['Schooling'], health['Life expectancy'], alpha=0.5, color='darkorange')
ax.set_xlabel('Real Average Years of Schooling')
ax.set_ylabel('Real Life Expectancy (Years)')
ax.set_title('Real Schooling vs Real Life Expectancy, All Country-Years')
plt.tight_layout()
plt.savefig('schooling_vs_life_expectancy.png', dpi=100)
plt.close()
Part 18: Real Biggest Life Expectancy Gains#
Change over time shows which countries made the biggest progress. The code builds 2000 and 2015 tables, renames life expectancy to le_2000 and le_2015, joins them with merge(on='Country'), and subtracts. Botswana gained most, from 47.8 to 65.7 years, a rise of 17.9. Bolivia gained 14.4, Afghanistan 10.2, Burkina Faso 8.9 and Bhutan 7.8. Large gains often come from a low starting point, so read them alongside where each country began.
early = health[health['Year'] == 2000][['Country', 'Life expectancy']].rename(columns={'Life expectancy': 'le_2000'})
late = health[health['Year'] == 2015][['Country', 'Life expectancy']].rename(columns={'Life expectancy': 'le_2015'})
le_change = early.merge(late, on='Country')
le_change['change'] = le_change['le_2015'] - le_change['le_2000']
le_change.sort_values('change', ascending=False).head(5)
Part 19: Real Smallest Life Expectancy Gains#
Sorting the same change column in ascending order shows the smallest gains. Brazil shows no change at all, 75.0 in both 2000 and 2015, which is unusual over 15 years and worth checking against another source. Argentina and Barbados gained 2.2 years, Bahrain 2.4 and Belize 2.7. Most of these started in the low to mid 70s, where further gains are typically slower.
le_change.sort_values('change').head(5)
Part 20: Real Missing Population Data#
Missing values are rarely random, so it helps to see which countries they belong to. The code filters rows where Population is missing and uses unique() to list the countries. The output shows 6: Antigua and Barbuda, the Bahamas, Bahrain, Barbados, Bolivia and Brunei Darussalam. Most are small island or small states. If you computed anything per head using Population, these countries would quietly drop out.
missing_pop_countries = health[health['Population'].isna()]['Country'].unique()
len(missing_pop_countries), missing_pop_countries
Part 21: Real Missing GDP Data#
GDP was one of the four predictors tested, so its gaps deserve a direct count. health['GDP'].isna().sum() returns 32 missing values, matching the audit. That is why the GDP correlation used dropna first. As a next step, you could check which countries these rows belong to, as you did for population, to see whether the GDP correlation is skewed by missing a particular group.
missing_gdp_count = health['GDP'].isna().sum()
missing_gdp_count
Part 22: Real Spending Gap by Development Status#
This compares the 2014 spending share for the two groups. spend_2014.groupby('Status')['Total expenditure'].mean() gives 7.66% for developed countries and 5.71% for developing ones. So developed countries do spend a larger share on health, by about two percentage points. Combined with the weak overall correlation, it suggests spending share alone does not explain the life expectancy gap.
spend_2014.groupby('Status')['Total expenditure'].mean().round(2)
Part 22b: Real Schooling Gap by Development Status#
Schooling was the strongest predictor, so it is worth seeing how it differs between the two groups. Using the 2015 latest table, the code groups by Status and averages Schooling. Developed countries average 17.0 years and developing countries 13.2, a gap of nearly four years. This lines up with the eight-year life expectancy gap seen earlier, although with only four developed countries the comparison is rough.
latest.groupby('Status')['Schooling'].mean().round(1)
Part 23: Real Life Expectancy Distribution#
The distribution of the outcome gives context for every comparison you have made. describe() on Life expectancy shows a mean of about 70.3 years and a median of 73.2, with values from 45.3 to 89.0. The mean sits below the median because a few very low values, such as Botswana's 47.8 in 2000, pull it down. The middle half of country-years fall between about 67.4 and 75.5 years.
health['Life expectancy'].describe()
Part 24: Real Full Correlation Matrix#
A correlation matrix computes every pairwise correlation at once, which is quicker than calling .corr() one pair at a time. health[corr_cols].corr() builds the full grid, and the code shows only the Life expectancy column. The values match the earlier results exactly: 0.108 for spending, 0.473 for GDP, 0.669 for schooling and 0.612 for income composition. That agreement confirms pandas was already skipping missing pairs. Look at the full corr_matrix too, to see how the predictors relate to each other.
corr_cols = ['Life expectancy', 'Total expenditure', 'GDP', 'Schooling', 'Income composition of resources']
corr_matrix = health[corr_cols].corr().round(3)
corr_matrix['Life expectancy']
Part 25: Saving the Real Enriched Dataset#
Saving a copy lets you start later analyses from a known state. to_csv with index=False writes the table without pandas' row numbers, and reading it back and comparing shapes gives True. Note this saves the table as loaded, including its missing values; the 2014 fallback is not stored, so record that choice in your notes.
health.to_csv('health_life_cleaned.csv', index=False)
reloaded = pd.read_csv('health_life_cleaned.csv')
reloaded.shape == health.shape
Part 26: Real Sanity Check, Row Count#
This assertion confirms the table still has the 416 rows seen at the start. The output is True. Short checks like this protect you from accidental filtering or duplication, and they flag immediately if a newer version of the file has a different size.
len(health) == 416
Part 27: Real Sanity Check, Full Year Coverage#
Every country should have the same number of years, otherwise trends and averages can shift when a country drops out. The code groups by Country, counts years with .count(), compares each with 16, and .all() returns True only if every country passes. The output np.True_ (numpy's True) confirms a complete, balanced panel of 26 countries by 16 years.
(health.groupby('Country')['Year'].count() == 16).all()
Part 28: Real Top Finding Print#
Generating the headline from the results means it stays correct if you rerun with different data. predictors is sorted from highest, so .index[0] gives the name and .iloc[0] the value of the strongest predictor. The output reads that Schooling was the strongest predictor of life expectancy in this dataset, at a correlation of 0.669, beating raw health spending share.
top_predictor = predictors.index[0]
top_predictor_value = predictors.iloc[0]
print(f'{top_predictor} was the real strongest predictor of life expectancy in this dataset, at a real correlation of {top_predictor_value}, beating raw health spending share.')
Part 29: Real Recap Print#
The recap sentence states the main contrast plainly. The f-string inserts the country count from nunique(), the spending correlation and the top predictor. The output reads: across 26 countries and sixteen years, health spending share correlated with life expectancy at just 0.108, while Schooling reached 0.669. It is a fair, cautious summary: it reports associations, not causes.
print(f'Across {health["Country"].nunique()} real countries and sixteen real years, health spending share correlated with life expectancy at just {spend_corr}, while {top_predictor} reached {top_predictor_value}.')
Wrap-Up: What You Learned#
- This real dataset honestly covers twenty-six countries and sixteen years, 2000 through 2015, not the full global set, and that real honest scoping matters before drawing conclusions.
- Real health spending as a share of GDP was a real surprisingly weak predictor of real life expectancy on its own, correlating at just about 0.1.
- Real schooling and real income composition were the real strongest predictors found, beating raw health spending by a real wide margin.
- Even a real famous, widely-used dataset has genuine reporting gaps, twenty-four of twenty-six countries were missing their real 2015 spending figure, requiring a real honest fallback to 2014.
- Real small island nations were disproportionately missing real population data, a real pattern worth remembering before trusting any per-capita figure.
- Next video: the real capstone for this domain, pulling together disease, spending, and outcome data into one real end-to-end healthcare analytics project.
Practice on your own
- Repeat the predictor comparison using only the 2014 rows in
spend_2014, and see whether schooling still beats spending share in a single year. - List the countries behind the 32 missing
GDPvalues, the same way the notebook did forPopulation, and check whether they overlap with the missing-population countries. - Merge
le_changewith each country's averageSchoolingand compute the correlation between schooling and life expectancy gain from 2000 to 2015.
Each exercise reuses patterns from this notebook. The next lesson in the series is the capstone for this domain, pulling disease, spending and outcome data together into one end-to-end healthcare analytics project.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



