GDP per country and evolution
‚Äč‚Äč
‚Äč
‚Äč
Tags: #worldbank #opendata
Author: Jeremy Ravenel‚Äč
Objective : allows to visualize the distribution of GDP per capita and the GDP growth in the world. Click on the country on the map or select it to see the details info
Data : GDP PER CAPITA (CURRENT US$) GDP GROWTH (ANNUAL %)
by countries, agregated by region
Sources:
World Bank national accounts data, OECD National Accounts data files.
Production : Team Denver 2020/04/20 (MyDigitalSchool)
source des données: data.worldbank.org
Introduction: https://drive.google.com/file/d/1kM7_P18bwEPrsZSk8YsvOdiuJyLN1_3H/view?usp=sharing

Input

Get the data

Récupération des données sur le PIB par pays: https://data.worldbank.org/indicator/NY.GDP.PCAP.CD
Récupération des données sur l'évolution du PIB par an par pays: https://data.worldbank.org/indicator/NY.GDP.PCAP.KD.ZG

Import libraries

1
import pandas as pd
2
import numpy as np
3
import plotly.graph_objects as go
Copied!

Model

Data formatting

1
from pandas_datareader import wb
2
‚Äč
3
indicators = wb.download(indicator=['NY.GDP.PCAP.CD', 'NY.GDP.PCAP.KD.ZG'], country='all', start=2013, end=2018)
4
‚Äč
5
indicators = indicators.reset_index()
6
indicators = indicators[['country', 'year', 'NY.GDP.PCAP.CD', 'NY.GDP.PCAP.KD.ZG']]
7
indicators.columns = ['country', 'year', 'GDP_PER_CAPITAL', 'GDP_GROWTH_PER_CAPITAL']
8
‚Äč
9
indicators = indicators.fillna(0)
10
‚Äč
11
countries = wb.get_countries()
12
countries = countries[['name', 'region', 'iso3c']]
13
‚Äč
14
master_table = pd.merge(indicators, countries, left_on='country', right_on='name')
15
‚Äč
16
master_table = master_table[master_table['region'] != 'Aggregates']
17
‚Äč
18
master_table = master_table.drop(columns=['name'])
19
‚Äč
20
master_table = master_table.dropna()
21
‚Äč
22
# Création de l'ensemble final
23
xls_formatted = pd.DataFrame(columns=['COUNTRY', 'YEAR', 'GDP_PER_CAPITAL', 'GDP_GROWTH_PER_CAPITAL', 'REGION', 'ISO3C'])
24
‚Äč
25
for index, line in master_table.iterrows():
26
xls_formatted = xls_formatted.append(
27
{
28
'COUNTRY': line['country'],
29
'YEAR': line['year'],
30
'GDP_PER_CAPITAL': line['GDP_PER_CAPITAL'],
31
'GDP_GROWTH_PER_CAPITAL': line['GDP_GROWTH_PER_CAPITAL'],
32
'REGION': line['region'],
33
'ISO3C': line['iso3c'],
34
}, ignore_index=True
35
)
36
‚Äč
37
master_table = xls_formatted
38
‚Äč
39
master_table
Copied!

Output

Display the plot with plotly

1
# Variable à changer pour avoir les autres années
2
year = "2018"
3
master_year_table = master_table[master_table['YEAR'] == year]
4
‚Äč
5
GDP_GROWTH_PER_CAPITAL = "GDP GROWTH PER CAPITAL"
6
GDP_PER_CAPITAL = "GDP PER CAPITAL"
7
‚Äč
8
fig = go.Figure()
9
‚Äč
10
fig.add_trace(go.Choropleth(
11
locations=master_year_table['ISO3C'],
12
z = master_year_table['GDP_PER_CAPITAL'],
13
colorscale = [(0,"black"), (0.01,"red"),(0.1,"yellow"),(0.3,"green"),(1,"green")],
14
colorbar_title = "GDP PER CAPITAL",
15
customdata = master_year_table['COUNTRY'],
16
hovertemplate = '<b>%{customdata}: %{z:,.0f}</b><extra></extra>'
17
))
18
‚Äč
19
fig.add_trace(go.Choropleth(
20
locations=master_year_table['ISO3C'],
21
visible= False,
22
z = master_year_table['GDP_GROWTH_PER_CAPITAL'],
23
colorscale = [(0,"red"),(0.5,"red"),(0.75,"rgb(240,230,140)"), (1,"green")],
24
colorbar_title = "GDP GROWTH PER CAPITAL",
25
customdata = master_year_table['COUNTRY'],
26
hovertemplate = '<b>%{customdata}: %{z:0.2f}%</b><extra></extra>'
27
))
28
‚Äč
29
fig.update_layout(
30
autosize=False,
31
width= 1600,
32
height= 900,
33
title=f"GDP per capital in {year}",
34
title_x=0.5,
35
updatemenus=[
36
dict(
37
type = "buttons",
38
active=0,
39
buttons=list([
40
dict(
41
args=[{"visible": [True, False]}, {"title": f"{GDP_PER_CAPITAL} in {year}"}],
42
label=GDP_PER_CAPITAL,
43
method="update"
44
),
45
dict(
46
args=[{"visible": [False, True]}, {"title": f"{GDP_GROWTH_PER_CAPITAL} in {year}"}],
47
label=GDP_GROWTH_PER_CAPITAL,
48
method="update"
49
)
50
]),
51
showactive=True,
52
x=1,
53
xanchor="right",
54
y=1.1,
55
yanchor="top"
56
),
57
]
58
)
59
‚Äč
60
fig.show()
Copied!
Copy link
Edit on GitHub