In [8]:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
unrate = pd.read_csv("un.csv") # open csv file with data from Stat Canada site
unrate
Out[8]:
Province May 2017 June 2017 Standard error{1} May to June 2017 June 2016 to June 2017 May to June 2017.1 June 2016 to June 2017.1
0 NaN thousands (except rates) thousands (except rates) thousands (except rates) change in thousands (except rates) change in thousands (except rates) % change % change
1 Newfoundland and Labrador NaN NaN NaN NaN NaN NaN NaN
2 Population 445.8 445.8 ... 0.0 1.3 0.0 0.3
3 Labour force 264.9 263.5 2.1 -1.4 -5.0 -0.5 -1.9
4 Employment 225.7 224.3 2.2 -1.4 -11.9 -0.6 -5.0
5 Full-time 193.1 192.9 2.6 -0.2 -6.8 -0.1 -3.4
6 Part-time 32.6 31.4 2.2 -1.2 -5.1 -3.7 -14.0
7 Unemployment 39.2 39.2 2.1 0.0 6.9 0.0 21.4
8 Participation rate 59.4 59.1 0.5 -0.3 -1.3 ... ...
9 Unemployment rate 14.8 14.9 0.7 0.1 2.9 ... ...
10 Employment rate 50.6 50.3 0.5 -0.3 -2.8 ... ...
11 NaN NaN NaN NaN NaN NaN NaN NaN
12 Prince Edward Island NaN NaN NaN NaN NaN NaN NaN
13 Population 123.7 123.8 ... 0.1 2.0 0.1 1.6
14 Labour force 83.3 83.0 0.6 -0.3 3.0 -0.4 3.8
15 Employment 75.0 74.5 0.7 -0.5 3.2 -0.7 4.5
16 Full-time 62.8 62.7 0.8 -0.1 3.5 -0.2 5.9
17 Part-time 12.2 11.8 0.7 -0.4 -0.3 -3.3 -2.5
18 Unemployment 8.3 8.5 0.6 0.2 -0.2 2.4 -2.3
19 Participation rate 67.3 67.0 0.5 -0.3 1.3 ... ...
20 Unemployment rate 10.0 10.2 0.7 0.2 -0.7 ... ...
21 Employment rate 60.6 60.2 0.5 -0.4 1.7 ... ...
22 NaN NaN NaN NaN NaN NaN NaN NaN
23 Nova Scotia NaN NaN NaN NaN NaN NaN NaN
24 Population 791.6 792.0 ... 0.4 3.6 0.1 0.5
25 Labour force 488.6 491.8 2.5 3.2 5.6 0.7 1.2
26 Employment 450.2 448.3 2.7 -1.9 1.5 -0.4 0.3
27 Full-time 366.2 365.4 3.5 -0.8 0.0 -0.2 0.0
28 Part-time 84.0 83.0 3.2 -1.0 1.7 -1.2 2.1
29 Unemployment 38.5 43.5 2.3 5.0 4.1 13.0 10.4
... ... ... ... ... ... ... ... ...
80 Labour force 608.6 607.8 2.6 -0.8 0.2 -0.1 0.0
81 Employment 570.3 568.4 2.6 -1.9 -1.7 -0.3 -0.3
82 Full-time 467.9 468.7 3.4 0.8 4.2 0.2 0.9
83 Part-time 102.4 99.7 3.1 -2.7 -5.9 -2.6 -5.6
84 Unemployment 38.3 39.4 2.2 1.1 1.9 2.9 5.1
85 Participation rate 69.4 69.3 0.3 -0.1 -0.7 ... ...
86 Unemployment rate 6.3 6.5 0.3 0.2 0.3 ... ...
87 Employment rate 65.1 64.8 0.3 -0.3 -0.9 ... ...
88 NaN NaN NaN NaN NaN NaN NaN NaN
89 Alberta NaN NaN NaN NaN NaN NaN NaN
90 Population 3425.5 3428.5 ... 3.0 29.4 0.1 0.9
91 Labour force 2484.2 2482.6 9.4 -1.6 39.0 -0.1 1.6
92 Employment 2291.1 2298.6 10.1 7.5 48.5 0.3 2.2
93 Full-time 1883.6 1872.2 13.7 -11.4 47.5 -0.6 2.6
94 Part-time 407.6 426.4 12.4 18.8 1.0 4.6 0.2
95 Unemployment 193.1 184.0 8.8 -9.1 -9.5 -4.7 -4.9
96 Participation rate 72.5 72.4 0.3 -0.1 0.5 ... ...
97 Unemployment rate 7.8 7.4 0.3 -0.4 -0.5 ... ...
98 Employment rate 66.9 67.0 0.3 0.1 0.8 ... ...
99 NaN NaN NaN NaN NaN NaN NaN NaN
100 British Columbia NaN NaN NaN NaN NaN NaN NaN
101 Population 3973.9 3979.0 ... 5.1 50.1 0.1 1.3
102 Labour force 2612.9 2620.4 10.6 7.5 88.0 0.3 3.5
103 Employment 2466.6 2486.3 10.4 19.7 104.3 0.8 4.4
104 Full-time 1931.6 1951.6 14.6 20.0 72.7 1.0 3.9
105 Part-time 535.0 534.7 14.0 -0.3 31.6 -0.1 6.3
106 Unemployment 146.3 134.1 9.0 -12.2 -16.3 -8.3 -10.8
107 Participation rate 65.8 65.9 0.3 0.1 1.4 ... ...
108 Unemployment rate 5.6 5.1 0.3 -0.5 -0.8 ... ...
109 Employment rate 62.1 62.5 0.3 0.4 1.9 ... ...

110 rows × 8 columns

In [9]:
list(unrate) # see the name of the collumns
Out[9]:
['Province',
 '\xa0May 2017',
 '\xa0June 2017',
 '\xa0Standard error{1}',
 '\xa0May to June 2017',
 '\xa0June 2016 to June 2017',
 '\xa0May to June 2017.1',
 '\xa0June 2016 to June 2017.1']
In [10]:
df1=unrate[(unrate['\xa0May 2017'].isnull())|(unrate['Province'] == 'Unemployment rate')]
df1=df1.fillna(method='bfill')
df1 
Out[10]:
Province May 2017 June 2017 Standard error{1} May to June 2017 June 2016 to June 2017 May to June 2017.1 June 2016 to June 2017.1
1 Newfoundland and Labrador 14.8 14.9 0.7 0.1 2.9 ... ...
9 Unemployment rate 14.8 14.9 0.7 0.1 2.9 ... ...
11 Prince Edward Island 10.0 10.2 0.7 0.2 -0.7 ... ...
12 Prince Edward Island 10.0 10.2 0.7 0.2 -0.7 ... ...
20 Unemployment rate 10.0 10.2 0.7 0.2 -0.7 ... ...
22 Nova Scotia 7.9 8.8 0.5 0.9 0.7 ... ...
23 Nova Scotia 7.9 8.8 0.5 0.9 0.7 ... ...
31 Unemployment rate 7.9 8.8 0.5 0.9 0.7 ... ...
33 New Brunswick 8.4 8.1 0.5 -0.3 -2.0 ... ...
34 New Brunswick 8.4 8.1 0.5 -0.3 -2.0 ... ...
42 Unemployment rate 8.4 8.1 0.5 -0.3 -2.0 ... ...
44 Quebec 6.0 6.0 0.3 0.0 -0.9 ... ...
45 Quebec 6.0 6.0 0.3 0.0 -0.9 ... ...
53 Unemployment rate 6.0 6.0 0.3 0.0 -0.9 ... ...
55 Ontario 6.5 6.4 0.2 -0.1 0.1 ... ...
56 Ontario 6.5 6.4 0.2 -0.1 0.1 ... ...
64 Unemployment rate 6.5 6.4 0.2 -0.1 0.1 ... ...
66 Manitoba 5.3 5.3 0.3 0.0 -0.9 ... ...
67 Manitoba 5.3 5.3 0.3 0.0 -0.9 ... ...
75 Unemployment rate 5.3 5.3 0.3 0.0 -0.9 ... ...
77 Saskatchewan 6.3 6.5 0.3 0.2 0.3 ... ...
78 Saskatchewan 6.3 6.5 0.3 0.2 0.3 ... ...
86 Unemployment rate 6.3 6.5 0.3 0.2 0.3 ... ...
88 Alberta 7.8 7.4 0.3 -0.4 -0.5 ... ...
89 Alberta 7.8 7.4 0.3 -0.4 -0.5 ... ...
97 Unemployment rate 7.8 7.4 0.3 -0.4 -0.5 ... ...
99 British Columbia 5.6 5.1 0.3 -0.5 -0.8 ... ...
100 British Columbia 5.6 5.1 0.3 -0.5 -0.8 ... ...
108 Unemployment rate 5.6 5.1 0.3 -0.5 -0.8 ... ...
In [11]:
df1=df1[~(df1['Province'] == 'Unemployment rate')]
df1=df1.drop_duplicates()
df1['\xa0June 2017'] = df1['\xa0June 2017'].astype(float)
df1['\xa0June 2016 to June 2017'] = df1['\xa0June 2016 to June 2017'].astype(float)
#df1['June/16']=df1[['\xa0June 2017','\xa0June 2016 to June 2017']].sum(axis=1)
df1['June 2016']=df1['\xa0June 2017']-df1['\xa0June 2016 to June 2017']
df1 = df1.reset_index(drop=True)
df1.dtypes
Out[11]:
Province                      object
 May 2017                     object
 June 2017                   float64
 Standard error{1}            object
 May to June 2017             object
 June 2016 to June 2017      float64
 May to June 2017.1           object
 June 2016 to June 2017.1     object
June 2016                    float64
dtype: object
In [12]:
df1
Out[12]:
Province May 2017 June 2017 Standard error{1} May to June 2017 June 2016 to June 2017 May to June 2017.1 June 2016 to June 2017.1 June 2016
0 Newfoundland and Labrador 14.8 14.9 0.7 0.1 2.9 ... ... 12.0
1 Prince Edward Island 10.0 10.2 0.7 0.2 -0.7 ... ... 10.9
2 Nova Scotia 7.9 8.8 0.5 0.9 0.7 ... ... 8.1
3 New Brunswick 8.4 8.1 0.5 -0.3 -2.0 ... ... 10.1
4 Quebec 6.0 6.0 0.3 0.0 -0.9 ... ... 6.9
5 Ontario 6.5 6.4 0.2 -0.1 0.1 ... ... 6.3
6 Manitoba 5.3 5.3 0.3 0.0 -0.9 ... ... 6.2
7 Saskatchewan 6.3 6.5 0.3 0.2 0.3 ... ... 6.2
8 Alberta 7.8 7.4 0.3 -0.4 -0.5 ... ... 7.9
9 British Columbia 5.6 5.1 0.3 -0.5 -0.8 ... ... 5.9
In [13]:
list(df1) # see the name of the collumns
Out[13]:
['Province',
 '\xa0May 2017',
 '\xa0June 2017',
 '\xa0Standard error{1}',
 '\xa0May to June 2017',
 '\xa0June 2016 to June 2017',
 '\xa0May to June 2017.1',
 '\xa0June 2016 to June 2017.1',
 'June 2016']
In [45]:
# Setting the positions and width for the bars
pos = list(range(len(df1['June 2016'])))
width = 0.3

# Plotting the bars
fig, ax = plt.subplots(figsize=(23,5))

# Create a bar with,
# in position pos,
plt.bar(pos,
        df1['June 2016'],
        width,
        alpha=0.5,
        color='blue',
        label=df1['Province'][0])

# Create a bar,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
        df1['\xa0June 2017'],
        width,
        alpha=0.5,
        color='green',
        label=df1['Province'][1])

ax.set_ylabel('Unemployment rate %') # Set the y axis label

ax.set_title('Canada Labour Force Survey, June 2017') # Set the title

ax.set_xticks([p + 0.5 * width for p in pos]) # Set the position of the x ticks

ax.set_xticklabels(df1['Province']) # Set the labels for the x ticks
 
plt.xlim(min(pos)-width, max(pos)+width*2) # Set the x-axis
plt.ylim([0, max(df1['June 2016'] +5 )]) # y-axis limits
 
plt.legend(['June 2016', '\xa0June 2017'], loc='upper right') # Add the legend
plt.grid()
plt.show() # Show the plot
In [41]:
df1
Out[41]:
Province May 2017 June 2017 Standard error{1} May to June 2017 June 2016 to June 2017 May to June 2017.1 June 2016 to June 2017.1 June 2016
0 Newfoundland and Labrador 14.8 14.9 0.7 0.1 2.9 ... ... 12.0
1 Prince Edward Island 10.0 10.2 0.7 0.2 -0.7 ... ... 10.9
2 Nova Scotia 7.9 8.8 0.5 0.9 0.7 ... ... 8.1
3 New Brunswick 8.4 8.1 0.5 -0.3 -2.0 ... ... 10.1
4 Quebec 6.0 6.0 0.3 0.0 -0.9 ... ... 6.9
5 Ontario 6.5 6.4 0.2 -0.1 0.1 ... ... 6.3
6 Manitoba 5.3 5.3 0.3 0.0 -0.9 ... ... 6.2
7 Saskatchewan 6.3 6.5 0.3 0.2 0.3 ... ... 6.2
8 Alberta 7.8 7.4 0.3 -0.4 -0.5 ... ... 7.9
9 British Columbia 5.6 5.1 0.3 -0.5 -0.8 ... ... 5.9
In [ ]: