{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Connected: None@factbook.db'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%capture\n",
"%load_ext sql\n",
"%sql sqlite:///factbook.db"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview of the Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll begin by getting a sense of what the data looks like."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"
\n",
" \n",
" id | \n",
" code | \n",
" name | \n",
" area | \n",
" area_land | \n",
" area_water | \n",
" population | \n",
" population_growth | \n",
" birth_rate | \n",
" death_rate | \n",
" migration_rate | \n",
"
\n",
" \n",
" 1 | \n",
" af | \n",
" Afghanistan | \n",
" 652230 | \n",
" 652230 | \n",
" 0 | \n",
" 32564342 | \n",
" 2.32 | \n",
" 38.57 | \n",
" 13.89 | \n",
" 1.51 | \n",
"
\n",
" \n",
" 2 | \n",
" al | \n",
" Albania | \n",
" 28748 | \n",
" 27398 | \n",
" 1350 | \n",
" 3029278 | \n",
" 0.3 | \n",
" 12.92 | \n",
" 6.58 | \n",
" 3.3 | \n",
"
\n",
" \n",
" 3 | \n",
" ag | \n",
" Algeria | \n",
" 2381741 | \n",
" 2381741 | \n",
" 0 | \n",
" 39542166 | \n",
" 1.84 | \n",
" 23.67 | \n",
" 4.31 | \n",
" 0.92 | \n",
"
\n",
" \n",
" 4 | \n",
" an | \n",
" Andorra | \n",
" 468 | \n",
" 468 | \n",
" 0 | \n",
" 85580 | \n",
" 0.12 | \n",
" 8.13 | \n",
" 6.96 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 5 | \n",
" ao | \n",
" Angola | \n",
" 1246700 | \n",
" 1246700 | \n",
" 0 | \n",
" 19625353 | \n",
" 2.78 | \n",
" 38.78 | \n",
" 11.49 | \n",
" 0.46 | \n",
"
\n",
"
"
],
"text/plain": [
"[(1, 'af', 'Afghanistan', 652230, 652230, 0, 32564342, 2.32, 38.57, 13.89, 1.51),\n",
" (2, 'al', 'Albania', 28748, 27398, 1350, 3029278, 0.3, 12.92, 6.58, 3.3),\n",
" (3, 'ag', 'Algeria', 2381741, 2381741, 0, 39542166, 1.84, 23.67, 4.31, 0.92),\n",
" (4, 'an', 'Andorra', 468, 468, 0, 85580, 0.12, 8.13, 6.96, 0.0),\n",
" (5, 'ao', 'Angola', 1246700, 1246700, 0, 19625353, 2.78, 38.78, 11.49, 0.46)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT *\n",
" FROM facts\n",
" LIMIT 5;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are the descriptions for some of the columns:\n",
"\n",
"* `name` - The name of the country.\n",
"* `area` - The total land and sea area of the country.\n",
"* `population` - The country's population.\n",
"* `population_growth`- The country's population growth as a percentage.\n",
"* `birth_rate` - The country's birth rate, or the number of births a year per 1,000 people.\n",
"* `death_rate` - The country's death rate, or the number of death a year per 1,000 people.\n",
"* `area`- The country's total area (both land and water).\n",
"* `area_land` - The country's land area in [square kilometers](https://www.cia.gov/library/publications/the-world-factbook/rankorder/2147rank.html).\n",
"* `area_water` - The country's waterarea in square kilometers.\n",
"\n",
"Let's start by calculating some summary statistics and see what they tell us."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary Statistics"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" min_pop | \n",
" max_pop | \n",
" min_pop_growth | \n",
" max_pop_growth | \n",
"
\n",
" \n",
" 0 | \n",
" 7256490011 | \n",
" 0.0 | \n",
" 4.02 | \n",
"
\n",
"
"
],
"text/plain": [
"[(0, 7256490011, 0.0, 4.02)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT MIN(population) AS min_pop,\n",
" MAX(population) AS max_pop,\n",
" MIN(population_growth) AS min_pop_growth,\n",
" MAX(population_growth) max_pop_growth \n",
" FROM facts;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A few things stick out from the summary statistics in the last screen:\n",
"\n",
"- There's a country with a population of `0`\n",
"- There's a country with a population of `7256490011` (or more than 7.2 billion people) \n",
"\n",
"Let's use subqueries to zoom in on just these countries _without_ using the specific values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploring Outliers"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" id | \n",
" code | \n",
" name | \n",
" area | \n",
" area_land | \n",
" area_water | \n",
" population | \n",
" population_growth | \n",
" birth_rate | \n",
" death_rate | \n",
" migration_rate | \n",
"
\n",
" \n",
" 250 | \n",
" ay | \n",
" Antarctica | \n",
" None | \n",
" 280000 | \n",
" None | \n",
" 0 | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[(250, 'ay', 'Antarctica', None, 280000, None, 0, None, None, None, None)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT *\n",
" FROM facts\n",
" WHERE population == (SELECT MIN(population)\n",
" FROM facts\n",
" );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It seems like the table contains a row for Antarctica, which explains the population of 0. This seems to match the CIA Factbook [page for Antarctica](https://www.cia.gov/library/publications/the-world-factbook/geos/ay.html):\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" id | \n",
" code | \n",
" name | \n",
" area | \n",
" area_land | \n",
" area_water | \n",
" population | \n",
" population_growth | \n",
" birth_rate | \n",
" death_rate | \n",
" migration_rate | \n",
"
\n",
" \n",
" 261 | \n",
" xx | \n",
" World | \n",
" None | \n",
" None | \n",
" None | \n",
" 7256490011 | \n",
" 1.08 | \n",
" 18.6 | \n",
" 7.8 | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[(261, 'xx', 'World', None, None, None, 7256490011, 1.08, 18.6, 7.8, None)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT *\n",
" FROM facts\n",
" WHERE population == (SELECT MAX(population)\n",
" FROM facts\n",
" );"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also see that the table contains a row for the whole world, which explains the maximum population of over 7.2 billion we found earlier.\n",
"\n",
"Now that we know this, we should recalculate the summary statistics we calculated earlier, while excluding the row for the whole world."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary Statistics Revisited"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" min_pop | \n",
" max_pop | \n",
" min_pop_growth | \n",
" max_pop_growth | \n",
"
\n",
" \n",
" 0 | \n",
" 1367485388 | \n",
" 0.0 | \n",
" 4.02 | \n",
"
\n",
"
"
],
"text/plain": [
"[(0, 1367485388, 0.0, 4.02)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT MIN(population) AS min_pop,\n",
" MAX(population) AS max_pop,\n",
" MIN(population_growth) AS min_pop_growth,\n",
" MAX(population_growth) AS max_pop_growth \n",
" FROM facts\n",
" WHERE name <> 'World';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's a country whose population closes in on 1.4 billion!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exploring Average Population and Area"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's explore density. Density depends on the population and the country's area. Let's look at the average values for these two columns.\n",
"\n",
"We should take care of discarding the row for the whole planet."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" avg_population | \n",
" avg_area | \n",
"
\n",
" \n",
" 32242666.56846473 | \n",
" 555093.546184739 | \n",
"
\n",
"
"
],
"text/plain": [
"[(32242666.56846473, 555093.546184739)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT AVG(population) AS avg_population, AVG(area) AS avg_area\n",
" FROM facts\n",
" WHERE name <> 'World';"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see that the average population is around 32 million and the average area is 555 thousand square kilometers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding Densely Populated Countries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To finish, we'll build on the query above to find countries that are densely populated. We'll identify countries that have:\n",
"\n",
"- Above average values for population.\n",
"- Below average values for area."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" id | \n",
" code | \n",
" name | \n",
" area | \n",
" area_land | \n",
" area_water | \n",
" population | \n",
" population_growth | \n",
" birth_rate | \n",
" death_rate | \n",
" migration_rate | \n",
"
\n",
" \n",
" 14 | \n",
" bg | \n",
" Bangladesh | \n",
" 148460 | \n",
" 130170 | \n",
" 18290 | \n",
" 168957745 | \n",
" 1.6 | \n",
" 21.14 | \n",
" 5.61 | \n",
" 0.46 | \n",
"
\n",
" \n",
" 65 | \n",
" gm | \n",
" Germany | \n",
" 357022 | \n",
" 348672 | \n",
" 8350 | \n",
" 80854408 | \n",
" 0.17 | \n",
" 8.47 | \n",
" 11.42 | \n",
" 1.24 | \n",
"
\n",
" \n",
" 80 | \n",
" iz | \n",
" Iraq | \n",
" 438317 | \n",
" 437367 | \n",
" 950 | \n",
" 37056169 | \n",
" 2.93 | \n",
" 31.45 | \n",
" 3.77 | \n",
" 1.62 | \n",
"
\n",
" \n",
" 83 | \n",
" it | \n",
" Italy | \n",
" 301340 | \n",
" 294140 | \n",
" 7200 | \n",
" 61855120 | \n",
" 0.27 | \n",
" 8.74 | \n",
" 10.19 | \n",
" 4.1 | \n",
"
\n",
" \n",
" 85 | \n",
" ja | \n",
" Japan | \n",
" 377915 | \n",
" 364485 | \n",
" 13430 | \n",
" 126919659 | \n",
" 0.16 | \n",
" 7.93 | \n",
" 9.51 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 91 | \n",
" ks | \n",
" Korea, South | \n",
" 99720 | \n",
" 96920 | \n",
" 2800 | \n",
" 49115196 | \n",
" 0.14 | \n",
" 8.19 | \n",
" 6.75 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 120 | \n",
" mo | \n",
" Morocco | \n",
" 446550 | \n",
" 446300 | \n",
" 250 | \n",
" 33322699 | \n",
" 1.0 | \n",
" 18.2 | \n",
" 4.81 | \n",
" 3.36 | \n",
"
\n",
" \n",
" 138 | \n",
" rp | \n",
" Philippines | \n",
" 300000 | \n",
" 298170 | \n",
" 1830 | \n",
" 100998376 | \n",
" 1.61 | \n",
" 24.27 | \n",
" 6.11 | \n",
" 2.09 | \n",
"
\n",
" \n",
" 139 | \n",
" pl | \n",
" Poland | \n",
" 312685 | \n",
" 304255 | \n",
" 8430 | \n",
" 38562189 | \n",
" 0.09 | \n",
" 9.74 | \n",
" 10.19 | \n",
" 0.46 | \n",
"
\n",
" \n",
" 163 | \n",
" sp | \n",
" Spain | \n",
" 505370 | \n",
" 498980 | \n",
" 6390 | \n",
" 48146134 | \n",
" 0.89 | \n",
" 9.64 | \n",
" 9.04 | \n",
" 8.31 | \n",
"
\n",
" \n",
" 173 | \n",
" th | \n",
" Thailand | \n",
" 513120 | \n",
" 510890 | \n",
" 2230 | \n",
" 67976405 | \n",
" 0.34 | \n",
" 11.19 | \n",
" 7.8 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 182 | \n",
" ug | \n",
" Uganda | \n",
" 241038 | \n",
" 197100 | \n",
" 43938 | \n",
" 37101745 | \n",
" 3.24 | \n",
" 43.79 | \n",
" 10.69 | \n",
" 0.74 | \n",
"
\n",
" \n",
" 185 | \n",
" uk | \n",
" United Kingdom | \n",
" 243610 | \n",
" 241930 | \n",
" 1680 | \n",
" 64088222 | \n",
" 0.54 | \n",
" 12.17 | \n",
" 9.35 | \n",
" 2.54 | \n",
"
\n",
" \n",
" 192 | \n",
" vm | \n",
" Vietnam | \n",
" 331210 | \n",
" 310070 | \n",
" 21140 | \n",
" 94348835 | \n",
" 0.97 | \n",
" 15.96 | \n",
" 5.93 | \n",
" 0.3 | \n",
"
\n",
"
"
],
"text/plain": [
"[(14, 'bg', 'Bangladesh', 148460, 130170, 18290, 168957745, 1.6, 21.14, 5.61, 0.46),\n",
" (65, 'gm', 'Germany', 357022, 348672, 8350, 80854408, 0.17, 8.47, 11.42, 1.24),\n",
" (80, 'iz', 'Iraq', 438317, 437367, 950, 37056169, 2.93, 31.45, 3.77, 1.62),\n",
" (83, 'it', 'Italy', 301340, 294140, 7200, 61855120, 0.27, 8.74, 10.19, 4.1),\n",
" (85, 'ja', 'Japan', 377915, 364485, 13430, 126919659, 0.16, 7.93, 9.51, 0.0),\n",
" (91, 'ks', 'Korea, South', 99720, 96920, 2800, 49115196, 0.14, 8.19, 6.75, 0.0),\n",
" (120, 'mo', 'Morocco', 446550, 446300, 250, 33322699, 1.0, 18.2, 4.81, 3.36),\n",
" (138, 'rp', 'Philippines', 300000, 298170, 1830, 100998376, 1.61, 24.27, 6.11, 2.09),\n",
" (139, 'pl', 'Poland', 312685, 304255, 8430, 38562189, 0.09, 9.74, 10.19, 0.46),\n",
" (163, 'sp', 'Spain', 505370, 498980, 6390, 48146134, 0.89, 9.64, 9.04, 8.31),\n",
" (173, 'th', 'Thailand', 513120, 510890, 2230, 67976405, 0.34, 11.19, 7.8, 0.0),\n",
" (182, 'ug', 'Uganda', 241038, 197100, 43938, 37101745, 3.24, 43.79, 10.69, 0.74),\n",
" (185, 'uk', 'United Kingdom', 243610, 241930, 1680, 64088222, 0.54, 12.17, 9.35, 2.54),\n",
" (192, 'vm', 'Vietnam', 331210, 310070, 21140, 94348835, 0.97, 15.96, 5.93, 0.3)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"SELECT *\n",
" FROM facts\n",
" WHERE population > (SELECT AVG(population)\n",
" FROM facts\n",
" WHERE name <> 'World'\n",
" )\n",
" AND area < (SELECT AVG(area)\n",
" FROM facts\n",
" WHERE name <> 'World'\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some of these countries are generally known to be densely populated, so we have confidence in our results!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}