-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback.py
More file actions
141 lines (97 loc) · 3.91 KB
/
back.py
File metadata and controls
141 lines (97 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import urllib.request, json
import json
import numpy as np
#import pycountry as pc
import pandas as pd
import csv
import itertools
import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
#EXTRACTING DATA AND CONVERTING IT TO A 1-DIMENSIONAL ARRAY
with urllib.request.urlopen("http://api.population.io:80/1.0/countries") as url:
slash = "/"
api_data = json.loads(url.read().decode())
twoD_data = np.array(list(api_data.values())) #turns the dictionary into a 2D array
all_countries = twoD_data.ravel()
#FORMATTING COUNTRIES FOR URL-CALLS LATER
country_code_data = pd.read_csv('https://pkgstore.datahub.io/core/country-list/data_csv/data/d7c9d7cfb42cb69f4422dec222dbbaa8/data_csv.csv')
df1 = country_code_data[['Name','Code']]
df2 = country_code_data[['Code']]
test = pd.read_csv("http://kejser.org/wp-content/uploads/2014/06/Country.csv",
delimiter='|')
df3 = test[["CountryName","Alpha3Code"]]
#print(test.head())
#print(df3)
#print(country_code_data)
country_and_countrycodes = df3.values
countrycodesTwoD = df2.values
countrycodes = countrycodesTwoD.ravel()
country_ready_for_fetch = []
chained_country_list = set(itertools.chain.from_iterable(country_and_countrycodes)) & set(all_countries)
for country in chained_country_list:
try:
if '/' not in country:
j = country.replace(" ","%20")
country_ready_for_fetch.append(j)
except AttributeError:
pass
#EXTRACTING THE POPULATION DATA
date = '/2013-01-01'
country_population = []
for country in country_ready_for_fetch:
url = "http://api.population.io:80/1.0/population/"
with urllib.request.urlopen(url + country + date) as url:
data = json.loads(url.read().decode())
country_population.append(country)
country_population.append(data)
country_population
newdate = '/2018-01-01'
country_popu = []
for country in country_ready_for_fetch:
url = "http://api.population.io:80/1.0/population/"
with urllib.request.urlopen(url + country + newdate) as url:
data = json.loads(url.read().decode())
country_popu.append(country)
country_popu.append(data)
country_popu
news = '/2018-01-01'
countryss = []
for country in country_ready_for_fetch:
url = "http://api.population.io:80/1.0/population/"
with urllib.request.urlopen(url + country + news) as url:
data = json.loads(url.read().decode())
countryss.append(country)
countryss.append(data)
countryss
result = {}
for i in range(0, len(country_population), 2):
result[country_population[i]] = country_population[i+1]['total_population']['population']
result = {key.replace('%20', ' '): value for key, value in result.items()}
l = np.array(country_and_countrycodes).tolist()
for i in l:
i.append(result.get(i[0]))
df = pd.DataFrame(l)
k = df.columns = ['Country','CountryCode','Population']
p = df['CountryCode'] = df['CountryCode'].str.upper()
data = dict(type = 'choropleth',
locations = df['CountryCode'],
z = df['Population'],
text = df['Country'],
colorscale='Portland',
colorbar = dict(title = 'Population in thousands'))
layout = dict(title = '2013 global Population',
geo = dict(showframe = False,
showlakes = True,
showrivers = True,
showcoastlines = True,
coastlinewidth = 6,
coastlinecolor = 'rgb(127,255,0)',
rivercolor = 'rgb(85,173,240)',
lakecolor = 'rgb(85,173,240)',
projection = {'type': 'stereographic'}))
choromap3 = go.Figure(data = [data], layout = layout)
df
exit = plot(choromap3)
print(exit)