World employment by sector
Tags: #worldbank #opendata
Author: Jeremy Ravenel
Objective
This graph compares the world distribution of employment by sector with the country distribution. Select the country to visualize which sector is dominant.
Data by countries, by region
Source International Labour Organization, ILOSTAT database.
Video : https://drive.google.com/file/d/1AISsc-lc4-94Dj7LOQKBj3L7VgChkejG/view

Input

Import library

1
import math
2
import pandas as pd
3
from datetime import datetime
4
from plotly.offline import iplot, plot, download_plotlyjs, init_notebook_mode
5
import plotly.graph_objects as go
6
from plotly.subplots import make_subplots
Copied!

Variables

1
URL = "https://www.ilo.org/ilostat-files/Documents/Excel/MBI_33_EN.xlsx"
2
3
SELECTED_YEAR = 2019
Copied!

Model

Retrieving data

1
retrieved_data = pd.read_excel(URL, sep='\t', parse_dates=[0],
2
names=['COUNTRY','b','GENDER','YEAR','e','AGRICULTURE - FORESTRY - FISHING','MINING - QUARRYING','MANUFACTURING','UTILITIES','CONSTRUCTION',
3
'WHOLESALE - RETAIL - REPAIR VEHICLES','TRANSPORT - STORAGE - COMMUNICATION','ACCOMODATION - FOOD SERVICES','FINANCE - INSURANCE',
4
'REAL ESTATE - BUSINESS - ADMINISTRATION','PUBLIC ADMINISTRATION - DEFENCE - SOCIAL SECURITY','EDUCATION','HUMAN HEALTH - SOCIAL WORK',
5
'OTHER SERVICES','t','u','v','w','x','y','z','aa','ab','ac','ad','ae','af','ag','ah'])
6
Copied!

Data selection

1
# Select only rows with data
2
data = retrieved_data.drop([0, 1, 2, 3, 4], axis = 0)
3
4
# Select rows we want ("Total" of gender and no male nor female)
5
data = data[data['GENDER'] == "Total"]
6
7
# Select by year
8
data = data[data['YEAR'] == SELECTED_YEAR]
9
10
# Select only columns with interest
11
df = data[['COUNTRY', 'AGRICULTURE - FORESTRY - FISHING','MINING - QUARRYING','MANUFACTURING','UTILITIES','CONSTRUCTION',
12
'WHOLESALE - RETAIL - REPAIR VEHICLES','TRANSPORT - STORAGE - COMMUNICATION','ACCOMODATION - FOOD SERVICES','FINANCE - INSURANCE',
13
'REAL ESTATE - BUSINESS - ADMINISTRATION','PUBLIC ADMINISTRATION - DEFENCE - SOCIAL SECURITY','EDUCATION','HUMAN HEALTH - SOCIAL WORK',
14
'OTHER SERVICES']]
15
16
df = df.set_index('COUNTRY')
17
df
Copied!

Output

Data formatting

1
# Initialization of variables and functions
2
3
SECTORS = []
4
for col in df.columns :
5
SECTORS.append(col)
6
7
indexstock = ['World', "France"]
8
9
#***********************************************************************************
10
# Initialization of graphs
11
12
fig = make_subplots(rows=2, cols=2,
13
specs=[[{'type': 'polar'}, {'type': 'polar'}],[{"colspan": 2}, None]],
14
shared_xaxes=True, shared_yaxes=False, row_heights=[0.3, 0.7],
15
vertical_spacing=0.11, horizontal_spacing=0.15)
16
17
#***********************************************************************************
18
# Add polar graphs
19
20
fig.add_trace(go.Scatterpolar(r = df.loc[indexstock[0]], theta = SECTORS, fill = 'toself', name = indexstock[0],
21
marker_color='mediumpurple'), row = 1, col = 1)
22
fig.add_trace(go.Scatterpolar(r = df.loc[indexstock[1]], theta = SECTORS, fill = 'toself', name = indexstock[1],
23
marker_color='indianred') ,row = 1, col = 2)
24
25
#***********************************************************************************
26
# Horizontal group bar graph
27
28
# print(df.loc['World'])
29
fig.add_trace(go.Bar(x=df.loc[indexstock[0]], y=SECTORS, orientation='h', name=indexstock[0], text=df.loc[indexstock[0]],
30
textposition='auto', marker_color='mediumpurple'), row = 2, col = 1)
31
fig.add_trace(go.Bar(x=df.loc[indexstock[1]], y=SECTORS, orientation='h', name=indexstock[1], text=df.loc[indexstock[1]],
32
textposition='auto', marker_color='indianred'), row = 2, col = 1)
33
34
#***********************************************************************************
35
# Setting layout
36
37
fig.update_layout(title_text="Differences of repartition of employement by country and sector au " + str(datetime.today()) + " (en %)",
38
title_x=0.5, width=1600, height=1000,
39
showlegend=True, legend=dict(x=-.2, y=0.8),
40
polar=dict(radialaxis=dict(visible=True)),
41
barmode='group')
42
43
#***********************************************************************************
44
# Creationg buttons
45
46
buttons_country_1 = []
47
for index in df.index:
48
buttons_country_1.append(dict(method='restyle', label=index, args=[{'r':[df.loc[index]], 'x':[df.loc[index]], 'name':[index, index], 'text':[df.loc[index]]}, [0, 2]]))
49
buttons_country_2 = []
50
for index in df.index:
51
buttons_country_2.append(dict(method='restyle', label=index, args=[{'r':[df.loc[index]], 'x':[df.loc[index]], 'name':[index, index], 'text':[df.loc[index]]}, [1, 3]]))
52
53
fig.update_layout(updatemenus=[dict(buttons=buttons_country_1, direction="down", pad={"r": 1, "t": 1}, showactive=True, x=0.04, xanchor="left", y=0.69, yanchor="top"),
54
dict(buttons=buttons_country_2, direction="down", pad={"r": 1, "t": 1}, showactive=True, x=0.6, xanchor="left", y=0.69, yanchor="top")])
55
56
#***********************************************************************************
57
# Display graph
58
59
fig.show()
60
61
#***********************************************************************************
62
# Export as HTML file
63
64
Tickets_plot = fig
65
plot(Tickets_plot, filename="/content/gdrive/My Drive/datamining/employement_by_sector_and_country.html", auto_open=False)
66
Copied!
Copy link
Edit on GitHub