Richest countries top10
Tags: #worldbank #opendata
Author: Jeremy Ravenel
Goal
Top 10 richest regions & countries in GDP per capita (wealth created per habitant) and in GDP current (total wealth created by the country)
Data
GDP CURRENT & GDP PER CAPITA by countries, agregated by region
Sources
  • World Bank national accounts data
  • OECD National Accounts data files
Notes
The top 10 for GDP current is including the G8, should the European Union be included in this ranking, it would come up 2nd biggest economy after the USA.
In the top 10 for GDP per capita, the ranking include smaller countries, only the USA remains in this ranking from the GDP current ranking.
Pitch
https://drive.google.com/file/d/1wGo9aI6mXS_2AbmnNlLoSJ9bGxkXmDhq/view

Input

Import libraries

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

Model

Get the association between the country and the region

1
pd.options.display.float_format = '{: .0f}'.format
2
countries = wb.get_countries()
3
countries = countries[['name', 'region']]
4
countries
Copied!

Get indicators

1
indicators = wb.download(indicator=['NY.GDP.PCAP.CD', 'NY.GDP.MKTP.CD'], country='all', start=2018, end=2018)
2
indicators = indicators.reset_index()
3
indicators = indicators[['country', 'NY.GDP.PCAP.CD', 'NY.GDP.MKTP.CD']]
4
indicators.columns = ['country', 'GDP_PER_CAPITA', 'CURRENT_GDP']
5
indicators
Copied!

Format a master table

  1. 1.
    Associate countries with regions
  2. 2.
    Clean up the data
  3. 3.
    Group rows by columns
1
master_table = pd.merge(indicators, countries, left_on='country', right_on='name')
2
3
master_table = master_table[master_table['region'] != 'Aggregates']
4
master_table = master_table[(master_table['GDP_PER_CAPITA'] > 0) | (master_table['CURRENT_GDP'] > 0)]
5
master_table = master_table.fillna(0)
6
7
master_table = pd.melt(master_table, id_vars=['region', 'country'], value_vars=['GDP_PER_CAPITA', 'CURRENT_GDP'], var_name='INDICATOR', value_name='VALUE')
8
master_table = master_table.set_index(['region', 'country', 'INDICATOR'])
9
master_table = master_table.sort_index()
10
11
master_table
Copied!

Output

Visualize data with a chart

1
table = master_table.reset_index()
2
gdp_per_capita_per_region = table[table['INDICATOR'] == 'GDP_PER_CAPITA'][['region', 'VALUE']].groupby('region').mean().sort_values('VALUE', ascending=False)
3
current_gdp_per_region = table[table['INDICATOR'] == 'CURRENT_GDP'][['region', 'VALUE']].groupby('region').mean().sort_values('VALUE', ascending=False)
4
gdp_per_capita_per_country = table[table['INDICATOR'] == 'GDP_PER_CAPITA'][['country', 'VALUE']].sort_values('VALUE', ascending=False).head(10)
5
current_gdp_per_country = table[table['INDICATOR'] == 'CURRENT_GDP'][['country', 'VALUE']].sort_values('VALUE', ascending=False).head(10)
6
7
data = [
8
go.Bar(x=gdp_per_capita_per_region.index, y=gdp_per_capita_per_region['VALUE'], text=gdp_per_capita_per_region['VALUE'], textposition='outside'),
9
go.Bar(x=current_gdp_per_region.index, y=current_gdp_per_region['VALUE'], text=current_gdp_per_region['VALUE'], textposition='outside', visible=False),
10
go.Bar(x=gdp_per_capita_per_country['country'], y=gdp_per_capita_per_country['VALUE'], text=gdp_per_capita_per_country['VALUE'], textposition='outside', visible=False),
11
go.Bar(x=current_gdp_per_country['country'], y=current_gdp_per_country['VALUE'], text=current_gdp_per_country['VALUE'], textposition='outside', visible=False),
12
]
13
14
layout = go.Layout(
15
title='Top 10 richest regions & countries',
16
margin = dict(t = 60, b = 150),
17
updatemenus=list([
18
dict(showactive=True, type="buttons", active=0, buttons=[
19
{'label': 'GDP / Capita per region', 'method': 'update', 'args': [{'visible': [True, False, False, False]}]},
20
{'label': 'Current GDP per region', 'method': 'update', 'args': [{'visible': [False, True, False, False]}]},
21
{'label': 'GDP / Capita per country', 'method': 'update', 'args': [{'visible': [False, False, True, False]}]},
22
{'label': 'Current GDP per country', 'method': 'update', 'args': [{'visible': [False, False, False, True]}]}
23
])
24
]),
25
annotations=[dict(
26
text = 'Updated in 2018 from The World Bank',
27
showarrow = False,
28
xref = 'paper', x = 1,
29
yref = 'paper', y = -0.4)]
30
)
31
32
go.Figure(data, layout)
Copied!
Copy link
Edit on GitHub