-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Code_File.txt
More file actions
152 lines (110 loc) · 4.15 KB
/
Python_Code_File.txt
File metadata and controls
152 lines (110 loc) · 4.15 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
141
142
143
144
145
146
147
148
149
150
151
152
!pip install cartopy
import cartopy
import numpy as np # useful for many scientific computing in Python
import pandas as pd # primary data structure library
#!pip3 install folium==0.5.0
import folium
print('Folium installed and imported!')
# define the world map
world_map = folium.Map()
# display world map
world_map
# define the world map centered around Canada with a higher zoom level
world_map = folium.Map(location=[56.130, -106.35], zoom_start=8)
# display world map
world_map
# create a Stamen Toner map of the world centered around Canada
world_map = folium.Map(location=[56.130, -106.35], zoom_start=4, tiles='Stamen Terrain')
# display map
world_map
from js import fetch
import io
URL = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/Canada.xlsx'
resp = await fetch(URL)
text = io.BytesIO((await resp.arrayBuffer()).to_py())
df_can = pd.read_excel(
text,
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
print('Data downloaded and read into a dataframe!')
import requests
import io
URL = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/Canada.xlsx'
# Fetch the data using requests library
response = requests.get(URL)
response.raise_for_status() # Raise an exception for bad responses (4xx or 5xx)
# Read the data into a BytesIO object
text = io.BytesIO(response.content)
df_can = pd.read_excel(
text,
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
print('Data downloaded and read into a dataframe!')
df_can.head()
# print the dimensions of the dataframe
print(df_can.shape)
# clean up the dataset to remove unnecessary columns (eg. REG)
df_can.drop(['AREA','REG','DEV','Type','Coverage'], axis=1, inplace=True)
# let's rename the columns so that they make sense
df_can.rename(columns={'OdName':'Country', 'AreaName':'Continent','RegName':'Region'}, inplace=True)
# for sake of consistency, let's also make all column labels of type string
df_can.columns = list(map(str, df_can.columns))
# add total column
df_can['Total'] = df_can.sum(axis=1, numeric_only=True)
# check how the dataset now looks like
df_can.head()
# years that we will be using in this lesson - useful for plotting later on
years = list(map(str, range(1980, 2014)))
print ('data dimensions:', df_can.shape)
df_can.head()
import requests
import io
import json
# ... (previous code remains the same) ...
# download countries geojson file
URL = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/world_countries.json'
# Fetch the data using requests library
response = requests.get(URL)
response.raise_for_status() # Raise an exception for bad responses (4xx or 5xx)
# Read the data into a BytesIO object
data = io.BytesIO(response.content)
world_geo = json.load(data)
print('GeoJSON file loaded!')
import folium
# ... (previous code remains the same) ...
# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=2)
# generate choropleth map using the total immigration of each country to Canada from 1980 to 2013
folium.GeoJson(
world_geo,
style_function=lambda feature: {
'fillColor': 'YlOrRd',
'fillOpacity': 0.7,
'color': 'black',
'weight': 0.2,
}
).add_to(world_map)
# add a legend (optional)
# ...
# display map
world_map
import folium
# ... (previous code remains the same) ...
# let Folium determine the scale.
world_map = folium.Map(location=[0, 0], zoom_start=2)
# Use folium.Choropleth instead of world_map.choropleth
folium.Choropleth(
geo_data=world_geo,
data=df_can,
columns=['Country', 'Total'],
key_on='feature.properties.name',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Immigration to Canada',
reset=True
).add_to(world_map) # Add the choropleth to the map
world_map