Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Created June 29, 2017 10:48
Show Gist options
  • Save saliksyed/ae531fd3fd4dbba9a76d5bd87325e33f to your computer and use it in GitHub Desktop.
Save saliksyed/ae531fd3fd4dbba9a76d5bd87325e33f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 29 11:26:56 2017
@author: saliksyed
"""
import csv
gdp_data = csv.reader(open("gdp.dat"))
gdp_by_country_code = {}
for row in gdp_data:
country = row[0]
country_code = row[1]
gdp_values = []
for value in row[5:]:
# We need to do this because there is some missing data which is represented by ""
if value == "":
final_parsed_value = None
else:
final_parsed_value = float(value)
gdp_values.append(final_parsed_value)
country_data = {}
country_data["country"] = country
country_data["country_code"] = country_code
country_data["gdp_values"] = gdp_values
gdp_by_country_code[country_code] = country_data
population_data = csv.reader(open("population.dat","r"))
population_by_country_code = {}
for row in population_data:
country = row[0]
country_code = row[1]
population_data = []
for value in row[5:]:
if value == "":
population_data.append(None)
else:
population_data.append(float(value))
country_data = {}
country_data["country"] = country
country_data["country_code"] = country_code
country_data["population_values"] = population_data
population_by_country_code[country_code] = country_data
per_capita_gdp_by_country_code = {}
for country_code in population_by_country_code.keys():
if country_code in population_by_country_code and country_code in gdp_by_country_code:
gdp = gdp_by_country_code[country_code]["gdp_values"]
population = population_by_country_code[country_code]["population_values"]
name = gdp_by_country_code[country_code]["country"]
gdp_per_capita = []
for i in xrange(0, len(gdp)):
if gdp[i] and population[i]:
gdp_per_capita.append(gdp[i]/population[i])
else:
gdp_per_capita.append(None)
country_data = {}
country_data["country"] = country
country_data["country_code"] = country_code
country_data["gdp_per_capita"] = gdp_per_capita
per_capita_gdp_by_country_code[country_code] = country_data
print per_capita_gdp_by_country_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment