Last active
May 29, 2018 11:29
-
-
Save manassra/d6aebeb804c09601ff98b497a3d7397f to your computer and use it in GitHub Desktop.
Joining Labor Force Data with the Shapefiles of Counties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import json | |
| import csv | |
| data = {} | |
| with open('labor-force.csv', 'r') as f: | |
| reader = csv.DictReader(f); | |
| for row in reader: | |
| print row | |
| statefip = int(row['state_fips']) | |
| countyfip = int(row['county_fips']) | |
| labor_force = 0 | |
| if (row['labor_force']): | |
| labor_force = int(row['labor_force']) | |
| employed = int(row['employed']) | |
| unemployment_level = int(row['unemployment_level']) | |
| unemployment_rate = float(row['unemployment_rate']) | |
| if statefip not in data: | |
| data[statefip] = {} | |
| data[statefip][countyfip] = { | |
| 'labor_force': labor_force, | |
| 'employed': employed, | |
| 'unemployment_level': unemployment_level, | |
| 'unemployment_rate': unemployment_rate | |
| } | |
| geojson = None | |
| with open('counties.geojson', 'r') as f: | |
| geojson = json.load(f) | |
| for feature in geojson['features']: | |
| featureProperties = feature['properties'] | |
| statefp = int(featureProperties['STATEFP']) | |
| countyfp = int(featureProperties['COUNTYFP']) | |
| featureData = data.get(statefp).get(countyfp, {}) | |
| for key in featureData.keys(): | |
| featureProperties[key] = featureData[key] | |
| with open('counties-unemployment.geojson', 'w') as f: | |
| json.dump(geojson, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment