├── LICENSE ├── README.md └── BLS_LAU_County.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 James Graham 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Clean BLS County Level Employment Data 2 | 3 | This file cleans employment and unemployment data by County from the U.S. Bureau of Labor Statistics (BLS). These data are known as Local Area Unemployment (LAU) statistics. The raw text files are not easy to immediately manipulate, so this cleaning file produces a "long format" .csv file for ease of use. 4 | 5 | # Getting Started 6 | 7 | BLS LAU data are available monthly from 1990-2017, and can be found at: . The code downloads the following raw data files from the BLS website: 8 | 9 | - la_area.txt 10 | - la_data_0_CurrentU90_94.txt 11 | - la_data_0_CurrentU95_99.txt 12 | - la_data_0_CurrentU00_04.txt 13 | - la_data_0_CurrentU05_09.txt 14 | - la_data_0_CurrentU10_14.txt 15 | - la_data_0_CurrentU15_19.txt 16 | 17 | Note that these files are each between 75mb and 131mb in size. These files can be deleted after cleaning, if the raw data is no longer needed. 18 | 19 | # Code output 20 | 21 | The code produces a .csv file with the following columns. 22 | 23 | - FIPS code 24 | - State 25 | - County name 26 | - Year 27 | - Month 28 | - Employment (level) 29 | - Labor force (level) 30 | - Unemployment (level) 31 | - Unemployment rate 32 | 33 | If all data from 1990-2017 is used, the resulting .csv file is approximately 60mb in size. 34 | 35 | # Prerequisites 36 | 37 | The script requires 'Python' along with the 'pandas' and 'requests' libraries. 38 | 39 | # Running the code 40 | 41 | The code can be run directly from the command line via: 42 | ``` 43 | python BLS_LAU_County.py 44 | ``` 45 | 46 | # Author 47 | 48 | - James Graham (NYU, 2018) 49 | 50 | # License 51 | 52 | This project is licensed under the MIT License. 53 | -------------------------------------------------------------------------------- /BLS_LAU_County.py: -------------------------------------------------------------------------------- 1 | #-------------------------------------- 2 | # 3 | # This file cleans employment ane unemployment data by County from the BLS Local 4 | # Area Unemployment statistics: https://download.bls.gov/pub/time.series/la/ 5 | # 6 | # The code produces a .csv with data for: 7 | # - Employment (level) 8 | # - Unemployment (level) 9 | # - Unemployment rate 10 | # - Labor force (level) 11 | # 12 | # Data are available monthly from 1990-2017 (can be updated with data from the website above). 13 | #-------------------------------------- 14 | 15 | import pandas as pd 16 | import requests 17 | 18 | #------------------------------------------------------ 19 | # Download and save .TXT files from BLS website into current directory 20 | 21 | BLS_url = 'https://download.bls.gov/pub/time.series/la/' 22 | 23 | filenames = ['la.area', 24 | 'la.data.0.CurrentU90-94', 'la.data.0.CurrentU95-99', 25 | 'la.data.0.CurrentU00-04', 'la.data.0.CurrentU05-09', 26 | 'la.data.0.CurrentU10-14','la.data.0.CurrentU15-19'] 27 | 28 | for xx in filenames: 29 | dls = BLS_url+xx 30 | resp = requests.get(dls) 31 | 32 | output = open(xx+'.txt', 'wb') 33 | output.write(resp.content) 34 | output.close() 35 | 36 | #------------------------------------------------------ 37 | # Import area information 38 | df_areas = pd.read_table('la.area.txt') 39 | df_areas = df_areas[['area_code', 'area_text']] 40 | 41 | # Only keep county information 42 | df_areas = df_areas.loc[df_areas['area_code'].str.contains('CN')] 43 | df_areas.reset_index(drop=True, inplace=True) 44 | 45 | # Rename columns 46 | df_areas.columns = ['area_code', 'countyname'] 47 | 48 | # Get county and state information 49 | tmp = df_areas['countyname'].str.split(', ', expand=True) 50 | df_areas['countyname'] = tmp[0] 51 | df_areas['state'] = tmp[1] 52 | 53 | # Remove whitespace 54 | df_areas['area_code'] = df_areas['area_code'].map(lambda x: x.strip()) 55 | df_areas['countyname'] = df_areas['countyname'].map(lambda x: x.strip()) 56 | # df_areas['state'] = df_areas['state'].map(lambda x: x.strip()) # Doesn't work when missing states? 57 | 58 | 59 | #------------------------------------------------------ 60 | 61 | def get_BLS_county_data(BLS_data_path, df_areas): 62 | ''' 63 | BLS_data_path : path for the text file containing the BLS data 64 | df_areas : dataframe containing BLS information about counties/areas 65 | ''' 66 | # Import area information 67 | col_types = {'series_id': str, 'year': int, 'period': str, 'value': str, 'footnote_codes': str} 68 | df_bls_county = pd.read_table(BLS_data_path, dtype=col_types) 69 | 70 | # Remove white space from code.. 71 | df_bls_county['series_id'] = df_bls_county['series_id'].map(lambda x: x.strip()) 72 | 73 | # Convert 'value' to numeric (kind of slow...) 74 | df_bls_county['value'] = df_bls_county['value'].apply(pd.to_numeric, errors='coerce') 75 | 76 | # Get variable code 77 | df_bls_county['var_code'] = df_bls_county['series_id'].str[-2:] 78 | 79 | # Get area code 80 | df_bls_county['series_id'] = df_bls_county['series_id'].astype(str).str[3:].str[:-2] 81 | 82 | # Get FIPS code (as string to preserve initial zeros) 83 | df_bls_county['FIPS'] = df_bls_county['series_id'].str[2:7] 84 | 85 | #------------------------------------------------------------ 86 | # Only keep rows corresponding to counties 87 | df_bls_county = df_bls_county.loc[df_bls_county['series_id'].str.contains('CN')] 88 | 89 | # Drop columns, reset index 90 | df_bls_county = df_bls_county[['series_id','year','period','value','var_code','FIPS']] 91 | df_bls_county.reset_index(drop=True, inplace=True) 92 | 93 | # Rename codes with variable names, rename columns 94 | df_bls_county['var_code'] = df_bls_county['var_code'].map({'03': 'Unemployment_Rate', '04': 'Unemployment', 95 | '05': 'Employment', '06': 'Labor_Force'}) 96 | df_bls_county.columns = ['area_code', 'year', 'month', 'value','variable_name', 'FIPS'] 97 | 98 | # Drop month 13 (I think this is the year average?) 99 | df_bls_county = df_bls_county.loc[df_bls_county['month']!='M13'] 100 | # Convert month to numeric values 101 | df_bls_county['month'] = pd.to_numeric(df_bls_county['month'].str[1:]) 102 | 103 | #------------------------------------------------------------ 104 | # Merge area names and data 105 | df_bls_county = pd.merge(df_bls_county, df_areas, how='inner', on='area_code') 106 | 107 | # Convert to wide-format table 108 | df_bls_county = df_bls_county.pivot_table(values='value', index=['area_code', 'FIPS', 'state', 'countyname', 109 | 'year', 'month'], columns='variable_name') 110 | df_bls_county.reset_index(inplace=True) 111 | df_bls_county.columns.name = None 112 | 113 | #------------------------------------------------------------ 114 | print('Done!') 115 | 116 | return df_bls_county 117 | 118 | #------------------------------------------------------------ 119 | # Import all years of data 120 | df_unemp_90_94 = get_BLS_county_data('la.data.0.CurrentU90-94.txt', df_areas) 121 | df_unemp_95_99 = get_BLS_county_data('la.data.0.CurrentU95-99.txt', df_areas) 122 | df_unemp_00_04 = get_BLS_county_data('la.data.0.CurrentU00-04.txt', df_areas) 123 | df_unemp_05_09 = get_BLS_county_data('la.data.0.CurrentU05-09.txt', df_areas) 124 | df_unemp_10_14 = get_BLS_county_data('la.data.0.CurrentU10-14.txt', df_areas) 125 | df_unemp_15_19 = get_BLS_county_data('la.data.0.CurrentU15-19.txt', df_areas) 126 | 127 | #------------------------------------------------------------ 128 | # Merge all year's data 129 | df_unemp_county = df_unemp_90_94 130 | df_unemp_county = df_unemp_county.append(df_unemp_95_99) 131 | df_unemp_county = df_unemp_county.append(df_unemp_00_04) 132 | df_unemp_county = df_unemp_county.append(df_unemp_05_09) 133 | df_unemp_county = df_unemp_county.append(df_unemp_10_14) 134 | df_unemp_county = df_unemp_county.append(df_unemp_15_19) 135 | 136 | # Sort by year-month 137 | df_unemp_county = df_unemp_county.sort_values(by=['area_code', 'year', 'month'], axis=0) 138 | 139 | # Save to CSV 140 | df_unemp_county[['FIPS', 'state', 'countyname', 'year', 'month', 141 | 'Employment', 'Labor_Force', 'Unemployment', 'Unemployment_Rate']].to_csv('BLS_county_employment.csv', index=False) 142 | --------------------------------------------------------------------------------