import webbrowser

import numpy as np
import pandas as pd
from folium import folium

df_can = pd.read_excel(
    'source/canada.xlsx',
    sheet_name='Canada by Citizenship',
    skiprows=range(20),
    skipfooter=2)

# 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)

# 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)

world_geo = r'source/country.json'  # geojson file

# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=2)

# create a numpy array of length 6 and has linear spacing from the minimum total immigration to the maximum total immigration
threshold_scale = np.linspace(df_can['Total'].min(),
                              df_can['Total'].max(),
                              6, dtype=int)
threshold_scale = threshold_scale.tolist()  # change the numpy array to a list
threshold_scale[-1] = threshold_scale[
                          -1] + 1  # make sure that the last value of the list is greater than the maximum immigration

# let Folium determine the scale.
world_map = folium.Map(location=[0, 0], zoom_start=2)
world_map.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
)


# display the map
def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


show_map(world_map)

438df0181aaa913d59b24099f513fb4ff1a661cf
<mask token>


def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


<mask token>

<mask token>
df_can.drop(['AREA', 'REG', 'DEV', 'Type', 'Coverage'], axis=1, inplace=True)
df_can.rename(columns={'OdName': 'Country', 'AreaName': 'Continent',
    'RegName': 'Region'}, inplace=True)
<mask token>
print('data dimensions:', df_can.shape)
<mask token>
world_map.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)


def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


show_map(world_map)

<mask token>
df_can = pd.read_excel('source/canada.xlsx', sheet_name=
    'Canada by Citizenship', skiprows=range(20), skipfooter=2)
df_can.drop(['AREA', 'REG', 'DEV', 'Type', 'Coverage'], axis=1, inplace=True)
df_can.rename(columns={'OdName': 'Country', 'AreaName': 'Continent',
    'RegName': 'Region'}, inplace=True)
df_can.columns = list(map(str, df_can.columns))
df_can['Total'] = df_can.sum(axis=1)
years = list(map(str, range(1980, 2014)))
print('data dimensions:', df_can.shape)
world_geo = 'source/country.json'
world_map = folium.Map(location=[0, 0], zoom_start=2)
threshold_scale = np.linspace(df_can['Total'].min(), df_can['Total'].max(),
    6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1] = threshold_scale[-1] + 1
world_map = folium.Map(location=[0, 0], zoom_start=2)
world_map.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)


def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


show_map(world_map)

import webbrowser
import numpy as np
import pandas as pd
from folium import folium
df_can = pd.read_excel('source/canada.xlsx', sheet_name=
    'Canada by Citizenship', skiprows=range(20), skipfooter=2)
df_can.drop(['AREA', 'REG', 'DEV', 'Type', 'Coverage'], axis=1, inplace=True)
df_can.rename(columns={'OdName': 'Country', 'AreaName': 'Continent',
    'RegName': 'Region'}, inplace=True)
df_can.columns = list(map(str, df_can.columns))
df_can['Total'] = df_can.sum(axis=1)
years = list(map(str, range(1980, 2014)))
print('data dimensions:', df_can.shape)
world_geo = 'source/country.json'
world_map = folium.Map(location=[0, 0], zoom_start=2)
threshold_scale = np.linspace(df_can['Total'].min(), df_can['Total'].max(),
    6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1] = threshold_scale[-1] + 1
world_map = folium.Map(location=[0, 0], zoom_start=2)
world_map.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)


def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


show_map(world_map)

import webbrowser

import numpy as np
import pandas as pd
from folium import folium

df_can = pd.read_excel(
    'source/canada.xlsx',
    sheet_name='Canada by Citizenship',
    skiprows=range(20),
    skipfooter=2)

# 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)

# 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)

world_geo = r'source/country.json'  # geojson file

# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=2)

# create a numpy array of length 6 and has linear spacing from the minimum total immigration to the maximum total immigration
threshold_scale = np.linspace(df_can['Total'].min(),
                              df_can['Total'].max(),
                              6, dtype=int)
threshold_scale = threshold_scale.tolist()  # change the numpy array to a list
threshold_scale[-1] = threshold_scale[
                          -1] + 1  # make sure that the last value of the list is greater than the maximum immigration

# let Folium determine the scale.
world_map = folium.Map(location=[0, 0], zoom_start=2)
world_map.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
)


# display the map
def show_map(world_map):
    world_map.save('worldmap.html')
    webbrowser.open('worldmap.html')


show_map(world_map)
