Gini index
Tags: #worldbank #opendata
Author: Jeremy Ravenel
Objective
The Gini index measures the equality per country. The closest to 0 the index is, the most equal the country is. The world average Gini index is at 38.
Data
GINI INDEX
Source
World Bank, Development Research Group.
Pitch
https://drive.google.com/file/d/10PXCX0Czck8QJwhinVEoKV3MZGvlAVDC/view

Input

Import library

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

Model

Get the association between the country and the ISO code

1
countries = wb.get_countries()
2
countries = countries[['name', 'iso3c']]
3
countries.columns = ['country', 'iso3c']
4
countries
Copied!

Get gini index indicator per country

1
indicators = wb.download(indicator=['SI.POV.GINI'], country='all', start=1967, end=2018)
2
indicators.columns = ['GINI_INDEX']
3
indicators
Copied!

Merge previous tables

1
master_table = pd.merge(indicators.reset_index(), countries, left_on='country', right_on='country')
2
master_table = master_table.set_index(['country', 'iso3c', 'year'])
3
master_table
Copied!

Pivot previous table and fill in undefined values with values from previous years

1
pivoted_table = pd.pivot_table(master_table, index=['country', 'iso3c'], columns='year', values='GINI_INDEX')
2
pivoted_table = pivoted_table.ffill(axis=1)
3
pivoted_table
Copied!

Show a map of gini index per country over the years (from 1969 to 2018)

1
pivoted_table = pd.pivot_table(master_table, index=['country', 'iso3c'], columns='year', values='GINI_INDEX')
2
pivoted_table = pivoted_table.ffill(axis=1)
3
countries = list(pivoted_table.index.get_level_values(0))
4
locations = list(pivoted_table.index.get_level_values(1))
5
6
data = []
7
steps = []
8
i = 0
9
for year in pivoted_table.columns:
10
data.append(dict(
11
type='choropleth',
12
name='',
13
locations=locations,
14
z=pivoted_table[year],
15
hovertext=countries,
16
colorscale=px.colors.sequential.Reds,
17
visible=year=='2018'
18
))
19
20
step = dict(
21
method='restyle',
22
args=['visible', [False] * len(pivoted_table.columns)],
23
label=year)
24
step['args'][1][i] = True
25
steps.append(step)
26
27
i = i + 1
28
29
layout = go.Layout(
30
title=dict(
31
text='Evolution of the gini index from 1969 to 2018',
32
x=0.5,
33
font=dict(
34
size=21,
35
)
36
),
37
sliders=[dict(steps=steps, active=len(data) - 1)],
38
annotations=[dict(
39
text='Updated in 2018 from The World Bank',
40
showarrow=False,
41
x=1,
42
y=-0.05
43
)],
44
autosize=True,
45
height=800
46
)
47
48
fig = go.Figure(data, layout)
49
fig
Copied!

Output

Export HTML

1
fig.write_html("file.html")
Copied!
Copy link
Edit on GitHub