︠ae0bc2c1-ed29-40b7-bcbf-b0d72ed975ffi︠
%md
# Data Analysis for Doughnut Economics
Celebrating the Doughnut. This worksheet is a calculation on *U.S. National Doughnut Day, June 1, 2018*, inspired by [Kate Raworth's Doughnut Economics](https://www.kateraworth.com/)
We download tables of
Gross Domestic Product (GDP) per capita and Human Development Index (HDI)
for the countries of the world and observe the discrepancy in the rankings.
There are more advanced visualizations Human Development Index (HDI) and related information.
For example, see [Our World in Data HDI](https://ourworldindata.org/human-development-index).
The purpose of this worksheet is to demonstrate basic access to the wealth of online data
using open-source programming tools (Python, pandas),
and to get a bit of a start at coding to explore the ideas of doughnut economics.
*Disclaimer: I am not an economist. (In case it wasn't obvious.)*
Feedback to Hal Snyder, drxyzzy@gmail.com
## Links
- Kate Raworth's blog posting: [Want to know how to get beyond GDP? Start here.](https://www.kateraworth.com/2012/07/01/want-to-know-how-to-get-beyond-gdp-start-here/)
- Why we might be interested in other measures of progress than GDP: [Beyond GDP: The Need for New Measures of Progress](http://www.oecd.org/site/progresskorea/globalproject/42613423.pdf) from 2009.
- Source for .xlsx file of HDI for 2015 is UN Development Programme [Human Development Data (1990-2015)](http://hdr.undp.org/en/data).
- Source for .csv file of GDP Per Capita in current USD for 2015 is at UN data [Per capita GDP at current prices - US dollars](http://data.un.org/Data.aspx?q=GDP+per+capita&d=SNAAMA&f=grID%3a101%3bcurrID%3aUSD%3bpcFlag%3a1).
︡01ca6dda-0870-44c0-a5f8-fe5582ccc1be︡{"done":true,"md":"\n# Data Analysis for Doughnut Economics\n\nCelebrating the Doughnut. This worksheet is a calculation on *U.S. National Doughnut Day, June 1, 2018*, inspired by [Kate Raworth's Doughnut Economics](https://www.kateraworth.com/)\n\n\n\nWe download tables of\nGross Domestic Product (GDP) per capita and Human Development Index (HDI)\nfor the countries of the world and observe the discrepancy in the rankings.\n\nThere are more advanced visualizations Human Development Index (HDI) and related information.\nFor example, see [Our World in Data HDI](https://ourworldindata.org/human-development-index).\n\nThe purpose of this worksheet is to demonstrate basic access to the wealth of online data\nusing open-source programming tools (Python, pandas),\nand to get a bit of a start at coding to explore the ideas of doughnut economics.\n\n*Disclaimer: I am not an economist. (In case it wasn't obvious.)*\n\nFeedback to Hal Snyder, drxyzzy@gmail.com\n\n## Links\n\n- Kate Raworth's blog posting: [Want to know how to get beyond GDP? Start here.](https://www.kateraworth.com/2012/07/01/want-to-know-how-to-get-beyond-gdp-start-here/)\n\n- Why we might be interested in other measures of progress than GDP: [Beyond GDP: The Need for New Measures of Progress](http://www.oecd.org/site/progresskorea/globalproject/42613423.pdf) from 2009.\n\n- Source for .xlsx file of HDI for 2015 is UN Development Programme [Human Development Data (1990-2015)](http://hdr.undp.org/en/data).\n\n- Source for .csv file of GDP Per Capita in current USD for 2015 is at UN data [Per capita GDP at current prices - US dollars](http://data.un.org/Data.aspx?q=GDP+per+capita&d=SNAAMA&f=grID%3a101%3bcurrID%3aUSD%3bpcFlag%3a1)."}
︠d8672c7a-78af-42af-9ca2-3f14dbf7b434s︠
%auto
%default_mode python3
︡95d62d48-4611-4c05-a367-0c5768b25d46︡{"done":true}︡
︠7d4c25ad-2a5e-474f-9f5f-4d68462f0661s︠
import sys
print(sys.version)
︡e59a32ea-1313-4e90-97b0-dbc73dfddaee︡{"stdout":"3.5.5 | packaged by conda-forge | (default, Feb 13 2018, 05:02:37) \n[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]\n"}︡{"done":true}︡
︠66724fd4-2a5e-4772-be7c-1f61a8c000acs︠
import pandas as pd
import requests
︡570dad08-fcd3-40dd-9fbd-069a593b7469︡{"done":true}︡
︠e69bd917-1084-4d1f-bd48-668042dcf2c2︠
# What version of pandas are we working with?
print(pd.__version__)
︡aee9c184-55db-48c8-bcc5-49a079c9143d︡{"stdout":"0.22.0\n"}︡{"done":true}︡
︠a638ec97-dc68-4ec7-87c1-60d7073c9630︠
# http://docs.python-requests.org/en/master/user/quickstart/
# Use python "requests" module to download the first spreadsheet.
data_url = "http://hdr.undp.org/sites/default/files/tables_1-15_6a_dashboard1_dashboard2_online_version.xlsx"
hdi_file = "hddata.xlsx"
r = requests.get(data_url, stream=True)
with open(hdi_file, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
︡dec98510-21d5-46d5-9b69-f20e7e7f86d0︡{"done":true}︡
︠8bb43e71-d9b1-4f9b-917c-cddb646e524f︠
# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html
# Fetch Excel file with lots of HDI data.
# We only need the spreadsheet labeled "Table 2".
x = pd.read_excel(hdi_file,
sheet_name="Table 2",
index_col=None,
usecols=[1,16],
header=None,
names=["country", "HDI2015"],
skiprows=6, skipfooter=240-197)
x.shape
︡89f4ffc9-4fcf-4722-b2e3-bbd8b7a2de61︡{"stdout":"(191, 2)"}︡{"done":true}︡
︠3436b461-7cf2-4345-a446-4ea738a2297f︠
# Look at first 3 rows to see what we have.
x.head(3)
︡aa65430b-01ea-4a0b-b11e-e1f5bd0dcef4︡{"html":"
\n\n
\n \n \n | \n country | \n HDI2015 | \n
\n \n \n \n 0 | \n Norway | \n 0.949423 | \n
\n \n 1 | \n Australia | \n 0.938680 | \n
\n \n 2 | \n Switzerland | \n 0.939131 | \n
\n \n
\n
"}︡{"done":true}︡
︠c951c946-d338-43d2-9aa6-605363055221s︠
# get per capita GDP for 2015, the most recent year with HDI listed per Table 2 above
# these numbers are at current prices - US dollars
# http://data.un.org/Data.aspx?q=GDP+per+capita&d=SNAAMA&f=grID%3a101%3bcurrID%3aUSD%3bpcFlag%3a1
# export to .csv and upload here to "gdppc2015.csv"
gdp_file = "gdppc2015.csv"
y = pd.read_csv(gdp_file, usecols=[0,3], names=["country","GDPPC2015"], header=0)
y.shape
︡04041b05-e6df-4a59-99b0-88b106985415︡{"stdout":"(212, 2)"}︡{"done":true}︡
︠0100b6f5-2517-45cf-a620-6a6271a856ed︠
# Have a look at the first 3 rows.
y.head(3)
︡bb187bf6-60d2-411f-9ab3-b68989de3a6f︡{"html":"\n\n
\n \n \n | \n country | \n GDPPC2015 | \n
\n \n \n \n 0 | \n Afghanistan | \n 610.854517 | \n
\n \n 1 | \n Albania | \n 3877.487975 | \n
\n \n 2 | \n Algeria | \n 4160.218690 | \n
\n \n
\n
"}︡{"done":true}︡
︠c662cce7-157d-4a50-9d81-2f2c1fc3c569︠
# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html
# Do inner join of the two tables on country name.
# This will drop countries that appear in only one of the tables.
z = x.merge(y, left_on='country', right_on='country').set_index("country")
z.shape
︡a6431bbf-fc0e-42e4-b3a8-95254f6631eb︡{"stdout":"(179, 2)"}︡{"done":true}︡
︠58de77c6-088b-4e4e-91c5-474c1dbc4923︠
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.scatter.html
# Scatter plot shows that HDI and GDP per capita are correlated, but the relationship
# is only vaguely monotonic, suggesting that high HDI is possible without commensurate
# per-capita GDP.
z.plot.scatter("HDI2015", "GDPPC2015", s=3);
︡dfa2bcd4-5eab-406e-94ab-c40f39a6e0cf︡{"file":{"filename":"/tmp/tmpcbpxbv.png","show":true,"text":null,"uuid":"5b1f9409-550a-4efe-829f-4e08d9cd6a30"},"once":false}︡{"done":true}︡
︠a3d95f7c-705a-4e31-b006-6c4114e99a76︠
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rank.html
# Create a new dataframe of ranks for each column,
# i.e. in column "HDI_rank", a value of 1.0 indicates the country with largest HDI
# 2.0 indicates the country with second-largest HDI, etc.
zr = z.rank(ascending=False)
zr.columns = ["HDI_rank","GDP_rank"]
zr.head(3)
︡755bd3cf-6ee7-4214-98c6-f868f5ccff79︡{"html":"\n\n
\n \n \n | \n HDI_rank | \n GDP_rank | \n
\n \n country | \n | \n | \n
\n \n \n \n Norway | \n 1.0 | \n 4.0 | \n
\n \n Australia | \n 3.0 | \n 10.0 | \n
\n \n Switzerland | \n 2.0 | \n 3.0 | \n
\n \n
\n
"}︡{"done":true}︡
︠2852bb08-3c9a-4c2e-8cb8-14ed6af40757︠
# Merge dataframe of values with dataframe of ranks.
zranked = z.merge(zr, left_index=True, right_index=True, sort=True)
zranked.head(3)
︡34bfd516-1c81-4bf3-88bf-6222ec9b83f4︡{"html":"\n\n
\n \n \n | \n HDI2015 | \n GDPPC2015 | \n HDI_rank | \n GDP_rank | \n
\n \n country | \n | \n | \n | \n | \n
\n \n \n \n Afghanistan | \n 0.479375 | \n 610.854517 | \n 161.0 | \n 167.0 | \n
\n \n Albania | \n 0.764174 | \n 3877.487975 | \n 71.0 | \n 107.0 | \n
\n \n Algeria | \n 0.744806 | \n 4160.218690 | \n 79.0 | \n 97.0 | \n
\n \n
\n
"}︡{"done":true}︡
︠772f2d44-6a74-4cc0-8efc-a19eebe547e2︠
# Sort by Human Development Index from greatest to least
# and display the top 15.
# Observe that GDP per capita rank is quite different from HDI rank
# for example, I live in the US, which ranks #7 in GDP per capita,
# but only #11 in HDI.
z_by_hdi = zranked.sort_values(["HDI2015"], ascending=False)
z_by_hdi.head(15)
︡94e87ba4-c235-4f91-bf26-b3fc329ade43︡{"html":"\n\n
\n \n \n | \n HDI2015 | \n GDPPC2015 | \n HDI_rank | \n GDP_rank | \n
\n \n country | \n | \n | \n | \n | \n
\n \n \n \n Norway | \n 0.949423 | \n 74360.641259 | \n 1.0 | \n 4.0 | \n
\n \n Switzerland | \n 0.939131 | \n 81647.599454 | \n 2.0 | \n 3.0 | \n
\n \n Australia | \n 0.938680 | \n 52237.928546 | \n 3.0 | \n 10.0 | \n
\n \n Germany | \n 0.925669 | \n 41313.210675 | \n 4.0 | \n 17.0 | \n
\n \n Singapore | \n 0.924866 | \n 53626.243222 | \n 5.0 | \n 8.0 | \n
\n \n Denmark | \n 0.924649 | \n 52964.425912 | \n 6.0 | \n 9.0 | \n
\n \n Netherlands | \n 0.924312 | \n 44750.095821 | \n 7.0 | \n 13.0 | \n
\n \n Ireland | \n 0.922746 | \n 61831.997993 | \n 8.0 | \n 6.0 | \n
\n \n Iceland | \n 0.921110 | \n 51241.827451 | \n 9.0 | \n 11.0 | \n
\n \n Canada | \n 0.920284 | \n 43193.886549 | \n 10.0 | \n 15.0 | \n
\n \n United States | \n 0.919553 | \n 56639.769525 | \n 11.0 | \n 7.0 | \n
\n \n New Zealand | \n 0.914900 | \n 38491.649589 | \n 12.0 | \n 20.0 | \n
\n \n Sweden | \n 0.912706 | \n 50997.572024 | \n 13.0 | \n 12.0 | \n
\n \n Liechtenstein | \n 0.911517 | \n 168177.258828 | \n 14.0 | \n 1.0 | \n
\n \n Japan | \n 0.903462 | \n 34224.423817 | \n 15.0 | \n 24.0 | \n
\n \n
\n
"}︡{"done":true}︡
︠cd4e284e-6bd0-4272-9735-ea07b0272522i︠
%md
### End of worksheet
︡f963d348-53b6-45b6-9d50-d6f7d6663395︡{"done":true,"md":"\n### End of worksheet"}
︠0f8d9d2f-8833-4aa4-b8ad-6627d03c5ccb︠