︠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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
countryHDI2015
0Norway0.949423
1Australia0.938680
2Switzerland0.939131
\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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
countryGDPPC2015
0Afghanistan610.854517
1Albania3877.487975
2Algeria4160.218690
\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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
HDI_rankGDP_rank
country
Norway1.04.0
Australia3.010.0
Switzerland2.03.0
\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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
HDI2015GDPPC2015HDI_rankGDP_rank
country
Afghanistan0.479375610.854517161.0167.0
Albania0.7641743877.48797571.0107.0
Algeria0.7448064160.21869079.097.0
\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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
HDI2015GDPPC2015HDI_rankGDP_rank
country
Norway0.94942374360.6412591.04.0
Switzerland0.93913181647.5994542.03.0
Australia0.93868052237.9285463.010.0
Germany0.92566941313.2106754.017.0
Singapore0.92486653626.2432225.08.0
Denmark0.92464952964.4259126.09.0
Netherlands0.92431244750.0958217.013.0
Ireland0.92274661831.9979938.06.0
Iceland0.92111051241.8274519.011.0
Canada0.92028443193.88654910.015.0
United States0.91955356639.76952511.07.0
New Zealand0.91490038491.64958912.020.0
Sweden0.91270650997.57202413.012.0
Liechtenstein0.911517168177.25882814.01.0
Japan0.90346234224.42381715.024.0
\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︠