-
Notifications
You must be signed in to change notification settings - Fork 0
/
unemployment.py
55 lines (45 loc) · 1.86 KB
/
unemployment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 09 13:34:28 2014
@author: Eric
Reads in uneployment data from http://www.bls.gov/lau/tables.htm
Suffixes at the end of variable names:
A: numpy array
B: boolean
D: dictionary
DF: pandas DataFrame
L: list
S: string
SR: pandas Series
T: tuple
Underscores indicate chaining: for instance, "fooT_T" is a tuple of tuples
"""
import os
import pandas as pd
import config
reload(config)
def main(fileNameS, year):
pathS = os.path.join(config.rawDataPathS, 'unemployment_statistics')
conversionD = {'state_fips_code': (lambda x: str(x)),
'county_fips_code': (lambda x: str(x)),
'year': (lambda x: int(x)),
'labor_force': (lambda x: int(str(x).translate(None, ','))),
'employed': (lambda x: int(str(x).translate(None, ','))),
'unemployed_level': (lambda x: int(str(x).translate(None, ',')))}
tableDF = pd.read_fwf(os.path.join(pathS, fileNameS),
converters=conversionD,
names=['laus_code', 'state_fips_code',
'county_fips_code', 'county_and_state',
'year', 'labor_force', 'employed',
'unemployed_level', 'unemployed_rate'],
skipfooter=3,
skiprows=6,
widths=[18, 7, 6, 50, 4, 14, 13, 11, 9])
tableDF.loc[:, 'fips_code'] = (tableDF.state_fips_code +
tableDF.county_fips_code).astype(int)
# Select relevant columns and set index
finalDF = tableDF.loc[:, ['fips_code', 'unemployed_rate']]
finalDF.columns = ['FIPS', 'URate' + str(year)]
finalDF = finalDF.sort(columns='FIPS')
finalDF = finalDF.set_index('FIPS')
return finalDF