j40-cejst-2/score/ipython/hud_recap_etl.ipynb
Lucas Merrill Brown 11d13e034e
Comparison tool refactor & ETL HUD RECAP (#272)
* Refactoring comparison tool and creating two new ETL notebooks
2021-07-06 12:10:58 -05:00

115 lines
3.4 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "20aa3891",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import numpy as np\n",
"import pandas as pd\n",
"import csv\n",
"import sys\n",
"import os\n",
"\n",
"module_path = os.path.abspath(os.path.join(\"..\"))\n",
"if module_path not in sys.path:\n",
" sys.path.append(module_path)\n",
"\n",
"from etl.sources.census.etl_utils import get_state_fips_codes\n",
"from utils import unzip_file_from_url, remove_all_from_dir\n",
"\n",
"DATA_PATH = Path.cwd().parent / \"data\"\n",
"TMP_PATH = DATA_PATH / \"tmp\"\n",
"HUD_RECAP_CSV_URL = \"https://opendata.arcgis.com/api/v3/datasets/56de4edea8264fe5a344da9811ef5d6e_0/downloads/data?format=csv&spatialRefId=4326\"\n",
"CSV_PATH = DATA_PATH / \"dataset\" / \"hud_recap\"\n",
"\n",
"# Definining some variable names\n",
"GEOID_TRACT_FIELD_NAME = \"GEOID10_TRACT\"\n",
"HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME = \"hud_recap_priority_community\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9455da5",
"metadata": {},
"outputs": [],
"source": [
"# Data from https://hudgis-hud.opendata.arcgis.com/datasets/HUD::racially-or-ethnically-concentrated-areas-of-poverty-r-ecaps/about\n",
"df = pd.read_csv(HUD_RECAP_CSV_URL, dtype={\"GEOID\": \"string\"})\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca63e66c",
"metadata": {},
"outputs": [],
"source": [
"# Rename some fields\n",
"df.rename(\n",
" columns={\n",
" \"GEOID\": GEOID_TRACT_FIELD_NAME,\n",
" # Interestingly, there's no data dictionary for the RECAP data that I could find.\n",
" # However, this site (http://www.schousing.com/library/Tax%20Credit/2020/QAP%20Instructions%20(2).pdf)\n",
" # suggests:\n",
" # \"If RCAP_Current for the tract in which the site is located is 1, the tract is an R/ECAP. If RCAP_Current is 0, it is not.\"\n",
" \"RCAP_Current\": HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME,\n",
" },\n",
" inplace=True,\n",
")\n",
"\n",
"# Convert to boolean\n",
"df[HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME] = df[\n",
" HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME\n",
"].astype(\"bool\")\n",
"\n",
"df[HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME].value_counts()\n",
"\n",
"df.sort_values(by=GEOID_TRACT_FIELD_NAME, inplace=True)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fa2077a",
"metadata": {},
"outputs": [],
"source": [
"# write csv\n",
"CSV_PATH.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# Drop unnecessary columns.\n",
"df[[GEOID_TRACT_FIELD_NAME, HUD_RECAP_PRIORITY_COMMUNITY_FIELD_NAME]].to_csv(\n",
" CSV_PATH / \"usa.csv\", index=False\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}