GDP contributors
‚Äč‚Äč
‚Äč
‚Äč
Tags: #worldbank #opendata
Author: Jeremy Ravenel‚Äč

Input

Import libraries

1
import pandas as pd
2
from pandas_datareader import wb
3
import plotly.graph_objects as go
Copied!

Model

Data recovery

1
country = wb.get_countries()["iso2c"]
2
startYear = 2017
3
endYear = startYear + 1
4
‚Äč
5
#Recupération des valeurs pour World (Initial - Final) GDP/PPL
6
data_gdp_world_2017 = wb.download(indicator=['NY.GDP.PCAP.KD.ZG'], country=['WLD'], start=startYear, end=startYear)
7
data_gdp_world_2018 = wb.download(indicator=['NY.GDP.PCAP.KD.ZG'], country=['WLD'], start=endYear, end=endYear)
8
‚Äč
9
gdp_growth_2017 = data_gdp_world_2017.iloc[0][0]
10
gdp_growth_2018 = data_gdp_world_2018.iloc[0][0]
11
‚Äč
12
data_gdp_current_year = wb.download(indicator=['NY.GDP.PCAP.KD.ZG'], country=country, start=endYear, end=endYear)
13
‚Äč
14
data_gdp_current_year
Copied!

Sort the data

1
data_gdp_biggest = data_gdp_current_year.sort_values('NY.GDP.PCAP.KD.ZG', ascending=False)
2
data_gdp_biggest = data_gdp_biggest.head(5)
3
‚Äč
4
data_gdp_lowest = data_gdp_current_year.sort_values('NY.GDP.PCAP.KD.ZG', ascending=True)
5
data_gdp_lowest = data_gdp_lowest.head(5)
Copied!

Formattage des données

1
#Data formating
2
‚Äč
3
measure = []
4
text = []
5
x = []
6
y = []
7
‚Äč
8
‚Äč
9
#Push initial
10
measure.append("absolute")
11
text.append(str(round(gdp_growth_2017,4)))
12
y.append(gdp_growth_2017)
13
x.append(str(startYear) + " Growth")
14
‚Äč
15
‚Äč
16
#Push Beetween
17
# measure.append("relative")
18
# text.append(str(round(gdp_growth_2018 - gdp_growth_2017,4)))
19
# y.append(gdp_growth_2018 - gdp_growth_2017)
20
# x.append("Between")
21
‚Äč
22
totalBiggest = 0
23
‚Äč
24
for index, row in data_gdp_biggest.iterrows() :
25
measure.append("relative")
26
text.append(index[0])
27
totalBiggest += row[0]
28
y.append(row[0])
29
x.append(index[0])
30
‚Äč
31
‚Äč
32
totalLowest = 0
33
‚Äč
34
for index, row in data_gdp_lowest.iterrows() :
35
measure.append("relative")
36
text.append(index[0])
37
totalLowest += row[0]
38
y.append(row[0])
39
x.append(index[0])
40
‚Äč
41
‚Äč
42
#Other Country total
43
otherCountryTotal = gdp_growth_2018 - (gdp_growth_2017 + totalLowest + totalBiggest)
44
measure.append("relative")
45
text.append(str(round(otherCountryTotal,4)))
46
y.append(otherCountryTotal)
47
x.append("Other Country")
48
‚Äč
49
‚Äč
50
‚Äč
51
#Push result
52
measure.append("total")
53
text.append(str(round(gdp_growth_2018,4)))
54
y.append(gdp_growth_2018)
55
x.append(str(endYear) + " Growth")
56
‚Äč
Copied!

Output

Plot display

Quand on passe le curseur sur chaque partie du graphique, on peut voir le PIB de l'année courrante, l'année précédente et la différence entre les deux du pays sélectionné.
Si la différence est positive, une flèche montante apparaite sinon une flèche descendante.
Au dessus de chaque pays, le pourcentage de croissance est représenté.
1
fig = go.Figure(go.Waterfall(
2
name = "Growth between Year", orientation = "v",
3
measure = measure,
4
x = x,
5
text = text,
6
y = y,
7
textposition = "outside",
8
texttemplate="%{y:.2f}%",
9
connector = {"line":{"color":"rgb(63, 63, 63)"}},
10
))
11
‚Äč
12
fig.update_layout(title = "GDP growth with 5 top and lowest contributors (per capita per country) ", showlegend = True, margin=dict(l=0, r=0, t=50, b=0),
13
height=700)
14
‚Äč
15
fig.show()
Copied!

Refaire les étapes pour le PIB par pays

1
#Recupération des valeurs pour World (Initial - Final) GDP/country
2
data_gdp_world_2017 = wb.download(indicator=['NY.GDP.MKTP.KD.ZG'], country=['WLD'], start=startYear, end=startYear)
3
data_gdp_world_2018 = wb.download(indicator=['NY.GDP.MKTP.KD.ZG'], country=['WLD'], start=endYear, end=endYear)
4
‚Äč
5
gdp_growth_pll_2017 = data_gdp_world_2017.iloc[0][0]
6
gdp_growth_2018 = data_gdp_world_2018.iloc[0][0]
7
‚Äč
8
data_gdp_current_year = wb.download(indicator=['NY.GDP.MKTP.KD.ZG'], country=country, start=endYear, end=endYear)
9
‚Äč
10
‚Äč
11
data_gdp_current_year
Copied!
1
data_gdp_biggest = data_gdp_current_year.sort_values('NY.GDP.MKTP.KD.ZG', ascending=False)
2
data_gdp_biggest = data_gdp_biggest.head(5)
3
‚Äč
4
data_gdp_lowest = data_gdp_current_year.sort_values('NY.GDP.MKTP.KD.ZG', ascending=True)
5
data_gdp_lowest = data_gdp_lowest.head(5)
Copied!
1
#Data formating
2
‚Äč
3
measure = []
4
text = []
5
x = []
6
y = []
7
‚Äč
8
‚Äč
9
#Push initial
10
measure.append("absolute")
11
text.append(str(round(gdp_growth_2017,4)))
12
y.append(gdp_growth_2017)
13
x.append(str(startYear) + " Growth")
14
‚Äč
15
‚Äč
16
#Push Beetween
17
# measure.append("relative")
18
# text.append(str(round(gdp_growth_2018 - gdp_growth_2017,4)))
19
# y.append(gdp_growth_2018 - gdp_growth_2017)
20
# x.append("Between")
21
‚Äč
22
totalBiggest = 0
23
‚Äč
24
for index, row in data_gdp_biggest.iterrows() :
25
measure.append("relative")
26
text.append(index[0])
27
totalBiggest += row[0]
28
y.append(row[0])
29
x.append(index[0])
30
‚Äč
31
‚Äč
32
totalLowest = 0
33
‚Äč
34
for index, row in data_gdp_lowest.iterrows() :
35
measure.append("relative")
36
text.append(index[0])
37
totalLowest += row[0]
38
y.append(row[0])
39
x.append(index[0])
40
‚Äč
41
‚Äč
42
#Other Country total
43
otherCountryTotal = gdp_growth_2018 - (gdp_growth_2017 + totalLowest + totalBiggest)
44
measure.append("relative")
45
text.append(str(round(otherCountryTotal,4)))
46
y.append(otherCountryTotal)
47
x.append("Other Country")
48
‚Äč
49
‚Äč
50
‚Äč
51
#Push result
52
measure.append("total")
53
text.append(str(round(gdp_growth_2018,4)))
54
y.append(gdp_growth_2018)
55
x.append(str(endYear) + " Growth")
Copied!
1
fig = go.Figure(go.Waterfall(
2
name = "Growth between Year", orientation = "v",
3
measure = measure,
4
x = x,
5
text = text,
6
y = y,
7
textposition = "outside",
8
texttemplate="%{y:.2f}%",
9
connector = {"line":{"color":"rgb(63, 63, 63)"}},
10
))
11
‚Äč
12
fig.update_layout(title = "GDP growth with 5 top and lowest contributors (per country)", showlegend = True,margin=dict(l=0, r=0, t=50, b=0), height=700)
13
‚Äč
14
fig.show()
Copied!
Copy link
Edit on GitHub