Goals

Some goals for this exercise:

  • to reacquaint ourselves with Python
  • to start learning how to use a particular IPython notebook environment, one which is easy to jump right into, namely Wakari
  • to learn a bit about the international context before diving into the US Census.
  • to get ourselves into looking at the Wikipedia as a data source.

Thinking about populations of various geographic entities is a good place to start with open data. We can to work with numbers without necessarily involving complicated mathematics. Just addition if we're lucky. We can also think about geographical locations. We can build out from our initial explorations in a systematica manner.

Things to Think About

Off the top of your head:

* What do you think is the current world population?
* How many countries are there?
* How many people are there in the USA?  Canada?  Mexico?  Your favorite country?
* What is the minimum number of countries to add up to 50% of the world's population?  How about 90%?

Now go answer these questions looking on the web. Find some a source or two or three.

Data Source for Populations

Two open sources we'll consider:

We will study how to parse these data sources in a later exercise, but for this exercise, the data sets have been parsed into JSON format, which is easily loadable in many languages, including Python using the json Python standard library. We'll also use requests.

Let's look first at the Wikipedia source.


In [2]:
# https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json
# scraping of https://en.wikipedia.org/w/index.php?title=List_of_countries_by_population_(United_Nations)&oldid=590438477

# read population in
import json
import requests

pop_json_url = "https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json"
pop_list= requests.get(pop_json_url).json()
pop_list


Out[2]:
[[1, u'China', 1385566537],
 [2, u'India', 1252139596],
 [3, u'United States', 320050716],
 [4, u'Indonesia', 249865631],
 [5, u'Brazil', 200361925],
 [6, u'Pakistan', 182142594],
 [7, u'Nigeria', 173615345],
 [8, u'Bangladesh', 156594962],
 [9, u'Russia', 142833689],
 [10, u'Japan', 127143577],
 [11, u'Mexico', 122332399],
 [12, u'Philippines', 98393574],
 [13, u'Ethiopia', 94100756],
 [14, u'Vietnam', 91679733],
 [15, u'Germany', 82726626],
 [16, u'Egypt', 82056378],
 [17, u'Iran', 77447168],
 [18, u'Turkey', 74932641],
 [19, u'Congo, Democratic Republic of the', 67513677],
 [20, u'Thailand', 67010502],
 [21, u'France', 64291280],
 [22, u'United Kingdom', 63136265],
 [23, u'Italy', 60990277],
 [24, u'Myanmar', 53259018],
 [25, u'South Africa', 52776130],
 [26, u'Korea, South', 49262698],
 [27, u'Tanzania', 49253126],
 [28, u'Colombia', 48321405],
 [29, u'Spain', 46926963],
 [30, u'Ukraine', 45238805],
 [31, u'Kenya', 44353691],
 [32, u'Argentina', 41446246],
 [33, u'Algeria', 39208194],
 [34, u'Poland', 38216635],
 [35, u'Sudan', 37964306],
 [36, u'Uganda', 37578876],
 [37, u'Canada', 35181704],
 [38, u'Iraq', 33765232],
 [39, u'Morocco', 33008150],
 [40, u'Afghanistan', 30551674],
 [41, u'Venezuela', 30405207],
 [42, u'Peru', 30375603],
 [43, u'Malaysia', 29716965],
 [44, u'Uzbekistan', 28934102],
 [45, u'Saudi Arabia', 28828870],
 [46, u'Nepal', 27797457],
 [47, u'Ghana', 25904598],
 [48, u'Mozambique', 25833752],
 [49, u'Korea, North', 24895480],
 [50, u'Yemen', 24407381],
 [51, u'Australia', 23342553],
 [52, u'Taiwan', 23329772],
 [53, u'Madagascar', 22924851],
 [54, u'Cameroon', 22253959],
 [55, u'Syria', 21898061],
 [56, u'Romania', 21698585],
 [57, u'Angola', 21471618],
 [58, u'Sri Lanka', 21273228],
 [59, u"C\xf4te d'Ivoire", 20316086],
 [60, u'Niger', 17831270],
 [61, u'Chile', 17619708],
 [62, u'Burkina Faso', 16934839],
 [63, u'Netherlands', 16759229],
 [64, u'Kazakhstan', 16440586],
 [65, u'Malawi', 16362567],
 [66, u'Ecuador', 15737878],
 [67, u'Guatemala', 15468203],
 [68, u'Mali', 15301650],
 [69, u'Cambodia', 15135169],
 [70, u'Zambia', 14538640],
 [71, u'Zimbabwe', 14149648],
 [72, u'Senegal', 14133280],
 [73, u'Chad', 12825314],
 [74, u'Rwanda', 11776522],
 [75, u'Guinea', 11745189],
 [76, u'South Sudan', 11296173],
 [77, u'Cuba', 11265629],
 [78, u'Greece', 11127990],
 [79, u'Belgium', 11104476],
 [80, u'Tunisia', 10996515],
 [81, u'Czech Republic', 10702197],
 [82, u'Bolivia', 10671200],
 [83, u'Portugal', 10608156],
 [84, u'Somalia', 10495583],
 [85, u'Dominican Republic', 10403761],
 [86, u'Benin', 10323474],
 [87, u'Haiti', 10317461],
 [88, u'Burundi', 10162532],
 [89, u'Hungary', 9954941],
 [90, u'Sweden', 9571105],
 [91, u'Serbia; Kosovo', 9510506],
 [92, u'Azerbaijan', 9413420],
 [93, u'Belarus', 9356678],
 [94, u'United Arab Emirates', 9346129],
 [95, u'Austria', 8495145],
 [96, u'Tajikistan', 8207834],
 [97, u'Honduras', 8097688],
 [98, u'Switzerland', 8077833],
 [99, u'Israel', 7733144],
 [100, u'Papua New Guinea', 7321262],
 [101, u'Jordan', 7273799],
 [102, u'Bulgaria', 7222943],
 [None, u'Hong Kong', 7203836],
 [103, u'Togo', 6816982],
 [104, u'Paraguay', 6802295],
 [105, u'Laos', 6769727],
 [106, u'El Salvador', 6340454],
 [107, u'Eritrea', 6333135],
 [108, u'Libya', 6201521],
 [109, u'Sierra Leone', 6092075],
 [110, u'Nicaragua', 6080478],
 [111, u'Denmark', 5619096],
 [112, u'Kyrgyzstan', 5547548],
 [113, u'Slovakia', 5450223],
 [114, u'Finland', 5426323],
 [115, u'Singapore', 5411737],
 [116, u'Turkmenistan', 5240072],
 [117, u'Norway', 5042671],
 [118, u'Costa Rica', 4872166],
 [119, u'Lebanon', 4821971],
 [120, u'Ireland', 4627173],
 [121, u'Central African Republic', 4616417],
 [122, u'New Zealand', 4505761],
 [123, u'Congo, Republic of the', 4447632],
 [124, u'Georgia', 4340895],
 [125, u'Palestine', 4326295],
 [126, u'Liberia', 4294077],
 [127, u'Croatia', 4289714],
 [128, u'Mauritania', 3889880],
 [129, u'Panama', 3864170],
 [130, u'Bosnia and Herzegovina', 3829307],
 [None, u'Puerto Rico', 3688318],
 [131, u'Oman', 3632444],
 [132, u'Moldova', 3487204],
 [133, u'Uruguay', 3407062],
 [134, u'Kuwait', 3368572],
 [135, u'Albania', 3173271],
 [136, u'Lithuania', 3016933],
 [137, u'Armenia', 2976566],
 [138, u'Mongolia', 2839073],
 [139, u'Jamaica', 2783888],
 [140, u'Namibia', 2303315],
 [141, u'Qatar', 2168673],
 [142, u'Macedonia', 2107158],
 [143, u'Lesotho', 2074465],
 [144, u'Slovenia', 2071997],
 [145, u'Latvia', 2050317],
 [146, u'Botswana', 2021144],
 [147, u'Gambia', 1849285],
 [148, u'Guinea-Bissau', 1704255],
 [149, u'Gabon', 1671711],
 [150, u'Trinidad and Tobago', 1341151],
 [151, u'Bahrain', 1332171],
 [152, u'Estonia', 1287251],
 [153, u'Swaziland', 1249514],
 [154, u'Mauritius', 1244403],
 [155, u'Cyprus', 1141166],
 [156, u'Timor-Leste', 1132879],
 [157, u'Fiji', 881065],
 [None, u'R\xe9union', 875375],
 [158, u'Djibouti', 872932],
 [159, u'Guyana', 799613],
 [160, u'Equatorial Guinea', 757014],
 [161, u'Bhutan', 753947],
 [162, u'Comoros', 734917],
 [163, u'Montenegro', 621383],
 [None, u'Western Sahara', 567315],
 [None, u'Macau', 566375],
 [164, u'Solomon Islands', 561231],
 [165, u'Suriname', 539276],
 [166, u'Luxembourg', 530380],
 [167, u'Cape Verde', 498897],
 [None, u'Guadeloupe', 465800],
 [168, u'Malta', 429004],
 [169, u'Brunei', 417784],
 [None, u'Martinique', 403682],
 [170, u'Bahamas', 377374],
 [171, u'Maldives', 345023],
 [172, u'Belize', 331900],
 [173, u'Iceland', 329535],
 [174, u'Barbados', 284644],
 [None, u'French Polynesia', 276831],
 [None, u'New Caledonia', 256496],
 [175, u'Vanuatu', 252763],
 [None, u'French Guiana', 249227],
 [None, u'Mayotte', 222152],
 [176, u'S\xe3o Tom\xe9 and Pr\xedncipe', 192993],
 [177, u'Samoa', 190372],
 [178, u'Saint Lucia', 182273],
 [None, u'Guam', 165124],
 [None, u'Guernsey; Jersey', 162018],
 [None, u'Cura\xe7ao', 158760],
 [179, u'Saint Vincent and the Grenadines', 109373],
 [None, u'Virgin Islands, United States', 106627],
 [180, u'Grenada', 105897],
 [181, u'Tonga', 105323],
 [182, u'Micronesia, Federated States of', 103549],
 [None, u'Aruba', 102911],
 [183, u'Kiribati', 102351],
 [184, u'Seychelles', 92838],
 [185, u'Antigua and Barbuda', 89985],
 [None, u'Isle of Man', 85888],
 [186, u'Andorra', 79218],
 [187, u'Dominica', 72003],
 [None, u'Bermuda', 65341],
 [None, u'Cayman Islands', 58435],
 [None, u'Greenland', 56987],
 [None, u'American Samoa', 55165],
 [188, u'Saint Kitts and Nevis', 54191],
 [None, u'Northern Mariana Islands', 53855],
 [189, u'Marshall Islands', 52634],
 [None, u'Faroe Islands', 49469],
 [None, u'Sint Maarten', 45233],
 [190, u'Monaco', 37831],
 [191, u'Liechtenstein', 36925],
 [None, u'Turks and Caicos Islands', 33098],
 [192, u'San Marino', 31448],
 [None, u'Gibraltar', 29310],
 [None, u'Virgin Islands, British', 28341],
 [193, u'Palau', 20918],
 [None, u'Cook Islands', 20629],
 [None, u'Caribbean Netherlands', 19130],
 [None, u'Anguilla', 14300],
 [None, u'Wallis and Futuna', 13272],
 [194, u'Nauru', 10051],
 [195, u'Tuvalu', 9876],
 [None, u'Saint Pierre and Miquelon', 6043],
 [None, u'Montserrat', 5091],
 [None, u'Saint Helena, Ascension and Tristan da Cunha', 4129],
 [None, u'Falkland Islands', 3044],
 [None, u'Niue', 1344],
 [None, u'Tokelau', 1195],
 [196, u'Vatican City', 799]]

EXERCISES

Show how to calculate the total population according to the list in the Wikipedia. (Answer: 7,162,119,434)


In [4]:
total_pop = 0

for i in pop_list:
    total_pop += i[2]

total_pop


Out[4]:
7162119434

Calculate the total population of 196 entities that have a numeric rank. (Answer: 7,145,999,288) BTW, are those entities actually countries?


In [10]:
ranked_pop = 0

for i in pop_list:
    if i[0]:
        ranked_pop += i[2]
        
ranked_pop


Out[10]:
7145999288

In [13]:
pop_json_url = "https://gist.github.com/rdhyee/8530164/raw/f8e842fe8ccd6e3bc424e3a24e41ef5c38f419e8/world_factbook_poulation.json"
fb_list= requests.get(pop_json_url).json()

fb_total=0
for i in fb_list:
    fb_total += i[2]

fb_total


Out[13]:
7600584210

CHALLENGE EXERCISE

Now for something more interesting. I'd like for us to get a feel of what it'd be like to pick a person completely at random from the world's population. Say, if you were picking 5 people -- where might these people be from? Of course, you won't be surprised to pick someone from China or India since those countries are so populous. But how likely will it be for someone from the USA to show up?

To the end of answering this question, start thinking about writing a Python generator that will return the name of a country in which the probability of that country being returned is the proportion of the world's population represented by that country.

Work with your neighbors -- we'll come back to this problem in detail in class on Thursday.


In [31]:
import random

def country_generator(inlist, max_pop):
    #print max_pop
    while 1:
        counted_pop = 0
        num = random.randint(1, max_pop)
        #print num
        for i in inlist:
            counted_pop += i[2]
            if counted_pop >= num:
                return i[1]


Out[31]:
[u'China',
 u'Iran',
 u'Egypt',
 u'India',
 u'Malawi',
 u'Chad',
 u'Nigeria',
 u'Belarus',
 u'China',
 u'Japan',
 u'Mexico',
 u'Indonesia',
 u'Chad',
 u'Nigeria',
 u'Indonesia',
 u'Kazakhstan',
 u'China',
 u'China',
 u'Armenia',
 u'United States',
 u'Iran',
 u'India',
 u'Bangladesh',
 u'China',
 u'France',
 u'India',
 u'Hungary',
 u'Germany',
 u'Cameroon',
 u'Philippines',
 u'Brazil',
 u'Indonesia',
 u'Indonesia',
 u'Israel',
 u'Canada',
 u'India',
 u'China',
 u'Russia',
 u'India',
 u'Turkmenistan',
 u'United States',
 u'United States',
 u'Morocco',
 u'China',
 u'India',
 u'India',
 u'Slovenia',
 u'India',
 u'Algeria',
 u'India',
 u'China',
 u'Iran',
 u'United States',
 u'Iran',
 u'Nigeria',
 u'Algeria',
 u'Bolivia',
 u'India',
 u'Korea, North',
 u'Tanzania',
 u'Korea, South',
 u'Brazil',
 u'Philippines',
 u'Kenya',
 u'Brazil',
 u'Mexico',
 u'Mexico',
 u'Afghanistan',
 u'Myanmar',
 u'Brazil',
 u'Nigeria',
 u'Japan',
 u'Lesotho',
 u'Brazil',
 u'China',
 u'Senegal',
 u'Spain',
 u'China',
 u'Ukraine',
 u'India',
 u'Netherlands',
 u'Indonesia',
 u'Indonesia',
 u'Nepal',
 u'Russia',
 u'Ghana',
 u'India',
 u'China',
 u'India',
 u"C\xf4te d'Ivoire",
 u'Pakistan',
 u'China',
 u'China',
 u'India',
 u'Turkey',
 u'United States',
 u'Japan',
 u'India',
 u'Sudan']

In [36]:
rand_countries = []
for i in range(1,100):
    rand_countries.append(country_generator(pop_list, total_pop))

rand_countries
c_dict = {}
for c in rand_countries:
    if c in c_dict:
        c_dict[c] += 1
    else:
        c_dict[c] = 1

c_dict


Out[36]:
{u'Angola': 1,
 u'Austria': 1,
 u'Bangladesh': 3,
 u'Brazil': 2,
 u'Burundi': 2,
 u'Canada': 2,
 u'Chad': 1,
 u'China': 19,
 u'Congo, Democratic Republic of the': 1,
 u'Egypt': 1,
 u'Ethiopia': 1,
 u'Finland': 1,
 u'France': 3,
 u'Germany': 1,
 u'Guinea': 1,
 u'India': 17,
 u'Indonesia': 3,
 u'Japan': 2,
 u'Korea, South': 1,
 u'Malaysia': 1,
 u'Mali': 1,
 u'Mexico': 3,
 u'Myanmar': 2,
 u'Nepal': 1,
 u'Netherlands': 1,
 u'Nigeria': 3,
 u'Pakistan': 1,
 u'Peru': 1,
 u'Philippines': 1,
 u'Poland': 2,
 u'Russia': 1,
 u'Serbia; Kosovo': 1,
 u'Sierra Leone': 1,
 u'Sudan': 2,
 u'Tanzania': 1,
 u'Thailand': 2,
 u'Turkey': 2,
 u'Uganda': 2,
 u'United Kingdom': 1,
 u'United States': 4,
 u'Venezuela': 1,
 u'Vietnam': 1}

In [ ]: