In [1]:
import plotly
import plotly.graph_objs as go
import numpy as np
plotly.offline.init_notebook_mode()

import pandas as pd



In [2]:
cat ../../Downloads/WPP2015_DB03_Population_Quinquennial.txt













































In [23]:
!file -I ../../Downloads/WPP2015_DB03_Population_Quinquennial.csv


Downloads/WPP2015_DB03_Population_Quinquennial.csv: text/plain; charset=us-ascii

In [25]:
!iconv -c -f us-ascii -t utf-8 ../../Downloads/WPP2015_DB03_Population_Quinquennial.csv > ../../Downloads/data_utf-8.csv

In [3]:
df = pd.read_csv("../../Downloads/data_utf-8.csv")
#df = pd.read_csv("Downloads/WPP2015_DB03_Population_Quinquennial.csv")

In [4]:
df


Out[4]:
LocID Location VarID Variant Time MidPeriod SexID Sex AgeGrp AgeGrpStart AgeGrpSpan Value
0 4 Afghanistan 2 Medium 1950 1950.5 1 Male 0-4 0 5 630.044
1 4 Afghanistan 2 Medium 1950 1950.5 1 Male 5-9 5 5 516.205
2 4 Afghanistan 2 Medium 1950 1950.5 1 Male 10-14 10 5 461.378
3 4 Afghanistan 2 Medium 1950 1950.5 1 Male 15-19 15 5 414.368
4 4 Afghanistan 2 Medium 1950 1950.5 1 Male 20-24 20 5 374.110
5 4 Afghanistan 2 Medium 1950 1950.5 1 Male 25-29 25 5 321.312
6 4 Afghanistan 2 Medium 1950 1950.5 1 Male 30-34 30 5 276.279
7 4 Afghanistan 2 Medium 1950 1950.5 1 Male 35-39 35 5 236.792
8 4 Afghanistan 2 Medium 1950 1950.5 1 Male 40-44 40 5 200.616
9 4 Afghanistan 2 Medium 1950 1950.5 1 Male 45-49 45 5 176.525
10 4 Afghanistan 2 Medium 1950 1950.5 1 Male 50-54 50 5 147.125
11 4 Afghanistan 2 Medium 1950 1950.5 1 Male 55-59 55 5 123.896
12 4 Afghanistan 2 Medium 1950 1950.5 1 Male 60-64 60 5 94.573
13 4 Afghanistan 2 Medium 1950 1950.5 1 Male 65-69 65 5 65.058
14 4 Afghanistan 2 Medium 1950 1950.5 1 Male 70-74 70 5 37.982
15 4 Afghanistan 2 Medium 1950 1950.5 1 Male 75-79 75 5 17.011
16 4 Afghanistan 2 Medium 1950 1950.5 1 Male 80+ 80 20 5.967
17 4 Afghanistan 2 Medium 1950 1950.5 2 Female 0-4 0 5 661.578
18 4 Afghanistan 2 Medium 1950 1950.5 2 Female 5-9 5 5 487.335
19 4 Afghanistan 2 Medium 1950 1950.5 2 Female 10-14 10 5 423.326
20 4 Afghanistan 2 Medium 1950 1950.5 2 Female 15-19 15 5 369.364
21 4 Afghanistan 2 Medium 1950 1950.5 2 Female 20-24 20 5 318.391
22 4 Afghanistan 2 Medium 1950 1950.5 2 Female 25-29 25 5 272.299
23 4 Afghanistan 2 Medium 1950 1950.5 2 Female 30-34 30 5 232.168
24 4 Afghanistan 2 Medium 1950 1950.5 2 Female 35-39 35 5 197.327
25 4 Afghanistan 2 Medium 1950 1950.5 2 Female 40-44 40 5 167.180
26 4 Afghanistan 2 Medium 1950 1950.5 2 Female 45-49 45 5 141.220
27 4 Afghanistan 2 Medium 1950 1950.5 2 Female 50-54 50 5 117.700
28 4 Afghanistan 2 Medium 1950 1950.5 2 Female 55-59 55 5 95.305
29 4 Afghanistan 2 Medium 1950 1950.5 2 Female 60-64 60 5 72.748
... ... ... ... ... ... ... ... ... ... ... ... ...
3505986 5501 Southern Asia 9 No change 2100 2100.5 2 Female 60-64 60 5 78915.639
3505987 5501 Southern Asia 9 No change 2100 2100.5 2 Female 65-69 65 5 68736.724
3505988 5501 Southern Asia 9 No change 2100 2100.5 2 Female 70-74 70 5 56055.250
3505989 5501 Southern Asia 9 No change 2100 2100.5 2 Female 75-79 75 5 40954.046
3505990 5501 Southern Asia 9 No change 2100 2100.5 2 Female 80-84 80 5 25628.437
3505991 5501 Southern Asia 9 No change 2100 2100.5 2 Female 85-89 85 5 12766.091
3505992 5501 Southern Asia 9 No change 2100 2100.5 2 Female 90-94 90 5 5222.857
3505993 5501 Southern Asia 9 No change 2100 2100.5 2 Female 95-99 95 5 1522.054
3505994 5501 Southern Asia 9 No change 2100 2100.5 2 Female 100+ 100 -1 363.748
3505995 5501 Southern Asia 9 No change 2100 2100.5 3 Both 0-4 0 5 336799.340
3505996 5501 Southern Asia 9 No change 2100 2100.5 3 Both 5-9 5 5 315867.869
3505997 5501 Southern Asia 9 No change 2100 2100.5 3 Both 10-14 10 5 299617.552
3505998 5501 Southern Asia 9 No change 2100 2100.5 3 Both 15-19 15 5 284973.163
3505999 5501 Southern Asia 9 No change 2100 2100.5 3 Both 20-24 20 5 270571.153
3506000 5501 Southern Asia 9 No change 2100 2100.5 3 Both 25-29 25 5 256291.283
3506001 5501 Southern Asia 9 No change 2100 2100.5 3 Both 30-34 30 5 242540.335
3506002 5501 Southern Asia 9 No change 2100 2100.5 3 Both 35-39 35 5 229835.136
3506003 5501 Southern Asia 9 No change 2100 2100.5 3 Both 40-44 40 5 218324.340
3506004 5501 Southern Asia 9 No change 2100 2100.5 3 Both 45-49 45 5 206706.644
3506005 5501 Southern Asia 9 No change 2100 2100.5 3 Both 50-54 50 5 193000.834
3506006 5501 Southern Asia 9 No change 2100 2100.5 3 Both 55-59 55 5 176368.201
3506007 5501 Southern Asia 9 No change 2100 2100.5 3 Both 60-64 60 5 157055.766
3506008 5501 Southern Asia 9 No change 2100 2100.5 3 Both 65-69 65 5 135282.885
3506009 5501 Southern Asia 9 No change 2100 2100.5 3 Both 70-74 70 5 108810.990
3506010 5501 Southern Asia 9 No change 2100 2100.5 3 Both 75-79 75 5 78140.567
3506011 5501 Southern Asia 9 No change 2100 2100.5 3 Both 80-84 80 5 47941.804
3506012 5501 Southern Asia 9 No change 2100 2100.5 3 Both 85-89 85 5 23383.865
3506013 5501 Southern Asia 9 No change 2100 2100.5 3 Both 90-94 90 5 9357.079
3506014 5501 Southern Asia 9 No change 2100 2100.5 3 Both 95-99 95 5 2677.819
3506015 5501 Southern Asia 9 No change 2100 2100.5 3 Both 100+ 100 -1 628.437

3506016 rows × 12 columns

First we do the following selection :

  • 2015 data
  • "Variant" = "Medium"
  • Sex : male + female
  • LocID (ISO-3) > 900 (only continent)

In [12]:
df2015 = df[(df["Time"] == 2015) & (df["Sex"] == "Both") & (df["Variant"] == "Medium") & (200 < df["LocID"]) & (df["LocID"] <= 250)]
df2015


Out[12]:
LocID Location VarID Variant Time MidPeriod SexID Sex AgeGrp AgeGrpStart AgeGrpSpan Value
86187 203 Czech Republic 2 Medium 2015 2015.5 3 Both 0-4 0 5 537.906
86188 203 Czech Republic 2 Medium 2015 2015.5 3 Both 5-9 5 5 568.736
86189 203 Czech Republic 2 Medium 2015 2015.5 3 Both 10-14 10 5 479.253
86190 203 Czech Republic 2 Medium 2015 2015.5 3 Both 15-19 15 5 452.632
86191 203 Czech Republic 2 Medium 2015 2015.5 3 Both 20-24 20 5 608.909
86192 203 Czech Republic 2 Medium 2015 2015.5 3 Both 25-29 25 5 694.583
86193 203 Czech Republic 2 Medium 2015 2015.5 3 Both 30-34 30 5 745.225
86194 203 Czech Republic 2 Medium 2015 2015.5 3 Both 35-39 35 5 921.292
86195 203 Czech Republic 2 Medium 2015 2015.5 3 Both 40-44 40 5 866.557
86196 203 Czech Republic 2 Medium 2015 2015.5 3 Both 45-49 45 5 698.852
86197 203 Czech Republic 2 Medium 2015 2015.5 3 Both 50-54 50 5 670.235
86198 203 Czech Republic 2 Medium 2015 2015.5 3 Both 55-59 55 5 668.736
86199 203 Czech Republic 2 Medium 2015 2015.5 3 Both 60-64 60 5 724.485
86200 203 Czech Republic 2 Medium 2015 2015.5 3 Both 65-69 65 5 678.067
86201 203 Czech Republic 2 Medium 2015 2015.5 3 Both 70-74 70 5 490.259
86202 203 Czech Republic 2 Medium 2015 2015.5 3 Both 75-79 75 5 308.944
86203 203 Czech Republic 2 Medium 2015 2015.5 3 Both 80-84 80 5 236.592
86204 203 Czech Republic 2 Medium 2015 2015.5 3 Both 85-89 85 5 133.771
86205 203 Czech Republic 2 Medium 2015 2015.5 3 Both 90-94 90 5 52.106
86206 203 Czech Republic 2 Medium 2015 2015.5 3 Both 95-99 95 5 5.207
86207 203 Czech Republic 2 Medium 2015 2015.5 3 Both 100+ 100 -1 0.839
88044 204 Benin 2 Medium 2015 2015.5 3 Both 0-4 0 5 1707.733
88045 204 Benin 2 Medium 2015 2015.5 3 Both 5-9 5 5 1523.648
88046 204 Benin 2 Medium 2015 2015.5 3 Both 10-14 10 5 1355.381
88047 204 Benin 2 Medium 2015 2015.5 3 Both 15-19 15 5 1172.421
88048 204 Benin 2 Medium 2015 2015.5 3 Both 20-24 20 5 1001.630
88049 204 Benin 2 Medium 2015 2015.5 3 Both 25-29 25 5 843.297
88050 204 Benin 2 Medium 2015 2015.5 3 Both 30-34 30 5 709.637
88051 204 Benin 2 Medium 2015 2015.5 3 Both 35-39 35 5 592.432
88052 204 Benin 2 Medium 2015 2015.5 3 Both 40-44 40 5 492.745
... ... ... ... ... ... ... ... ... ... ... ... ...
106626 246 Finland 2 Medium 2015 2015.5 3 Both 60-64 60 5 368.737
106627 246 Finland 2 Medium 2015 2015.5 3 Both 65-69 65 5 407.749
106628 246 Finland 2 Medium 2015 2015.5 3 Both 70-74 70 5 239.669
106629 246 Finland 2 Medium 2015 2015.5 3 Both 75-79 75 5 196.876
106630 246 Finland 2 Medium 2015 2015.5 3 Both 80-84 80 5 146.452
106631 246 Finland 2 Medium 2015 2015.5 3 Both 85-89 85 5 92.263
106632 246 Finland 2 Medium 2015 2015.5 3 Both 90-94 90 5 36.385
106633 246 Finland 2 Medium 2015 2015.5 3 Both 95-99 95 5 6.825
106634 246 Finland 2 Medium 2015 2015.5 3 Both 100+ 100 -1 0.734
108471 250 France 2 Medium 2015 2015.5 3 Both 0-4 0 5 3926.920
108472 250 France 2 Medium 2015 2015.5 3 Both 5-9 5 5 3986.544
108473 250 France 2 Medium 2015 2015.5 3 Both 10-14 10 5 3989.071
108474 250 France 2 Medium 2015 2015.5 3 Both 15-19 15 5 3802.700
108475 250 France 2 Medium 2015 2015.5 3 Both 20-24 20 5 3652.962
108476 250 France 2 Medium 2015 2015.5 3 Both 25-29 25 5 3902.296
108477 250 France 2 Medium 2015 2015.5 3 Both 30-34 30 5 3992.084
108478 250 France 2 Medium 2015 2015.5 3 Both 35-39 35 5 3872.985
108479 250 France 2 Medium 2015 2015.5 3 Both 40-44 40 5 4335.701
108480 250 France 2 Medium 2015 2015.5 3 Both 45-49 45 5 4301.844
108481 250 France 2 Medium 2015 2015.5 3 Both 50-54 50 5 4308.345
108482 250 France 2 Medium 2015 2015.5 3 Both 55-59 55 5 4074.735
108483 250 France 2 Medium 2015 2015.5 3 Both 60-64 60 5 3936.439
108484 250 France 2 Medium 2015 2015.5 3 Both 65-69 65 5 3791.252
108485 250 France 2 Medium 2015 2015.5 3 Both 70-74 70 5 2431.887
108486 250 France 2 Medium 2015 2015.5 3 Both 75-79 75 5 2193.359
108487 250 France 2 Medium 2015 2015.5 3 Both 80-84 80 5 1892.085
108488 250 France 2 Medium 2015 2015.5 3 Both 85-89 85 5 1251.442
108489 250 France 2 Medium 2015 2015.5 3 Both 90-94 90 5 638.155
108490 250 France 2 Medium 2015 2015.5 3 Both 95-99 95 5 94.771
108491 250 France 2 Medium 2015 2015.5 3 Both 100+ 100 -1 19.768

273 rows × 12 columns


In [13]:
df2015 = df2015.groupby(["Location", "LocID", "Time", "Sex", "Variant"], as_index=False).aggregate({"Value": np.sum})
df2015 = df2015.set_index("Location")
df2015


Out[13]:
LocID Time Sex Variant Value
Location
Benin 204 2015 Both Medium 10879.829
Czech Republic 203 2015 Both Medium 10543.186
Denmark 208 2015 Both Medium 5669.081
Dominican Republic 214 2015 Both Medium 10528.391
Ecuador 218 2015 Both Medium 16144.363
El Salvador 222 2015 Both Medium 6126.583
Equatorial Guinea 226 2015 Both Medium 845.060
Eritrea 232 2015 Both Medium 5227.791
Estonia 233 2015 Both Medium 1312.558
Ethiopia 231 2015 Both Medium 99390.750
Fiji 242 2015 Both Medium 892.145
Finland 246 2015 Both Medium 5503.457
France 250 2015 Both Medium 64395.345

In [7]:
df_continent = df2015.loc[["AFRICA", "ASIA", "EUROPE", "NORTHERN AMERICA", "OCEANIA"]]
df_continent


Out[7]:
LocID Time Sex Variant Value
Location
AFRICA 903 2015 Both Medium 1186178.282
ASIA 935 2015 Both Medium 4393296.014
EUROPE 908 2015 Both Medium 738442.070
NORTHERN AMERICA 905 2015 Both Medium 357838.036
OCEANIA 909 2015 Both Medium 39331.130

In [22]:
colorscale = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],
              [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
data = [
    dict(
        type='choropleth',
        colorscale = colorscale,
        autocolorscale = False,
        locations = df2015.index,
        z = df2015['Value'],
        locationmode = 'country name',
        text = df2015.index.astype(str) + " " + str(df2015["Value"]),
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "Population")
        ) ]

layout = dict(
        title = 'World population by countries',
        geo = dict(
            scope='world',
            projection=dict( type='natural earth' )
        )
    )
    
fig = dict( data=data, layout=layout )
plotly.plotly.iplot( fig, filename='world-map' )
#plotly.plotly.image.save_as(fig, "d3-map.png")


Out[22]:

In [19]:
colorscale = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],
              [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
data = [
    dict(
        type='choropleth',
        colorscale = colorscale,
        autocolorscale = False,
        locations = df_continent["LocID"],
        z = df_continent['Value'],
        locationmode = 'country name',
        text = df_continent.index.astype(str) + " " + str(df_continent["Value"]),
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "Population")
        ) ]

layout = dict(
        title = 'World population by countries',
        geo = dict(
            scope='world',
            projection=dict( type='orthographic' )
        )
    )
    
fig = dict( data=data, layout=layout )
plotly.plotly.iplot( fig, filename='world-map' )
#plotly.plotly.image.save_as(fig, "d3-map.png")


Out[19]: