{ "cells": [ { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "import getpass\n", "user = getpass.getuser()\n", "DVUTILS_LOCAL_CLONE_PATH = f\"/Users/{user}/Documents/GitHub/dvutils\"\n", "sys.path.insert(0, DVUTILS_LOCAL_CLONE_PATH)\n", "from utils_io import *\n", "\n", "CENSUS_UTILS_PATH = f\"/Users/{user}/Documents/GitHub/hess_application/data_processing/6 - affh\"\n", "sys.path.insert(0, CENSUS_UTILS_PATH)\n", "import census\n", "\n", "import pandas as pd\n", "import geopandas as gpd" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Pull Census Data" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "census_api = getpass.getpass()" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "lep_df = census.pull_acs_5_year_est_data(\n", " census_api_key=census_api,\n", " acs_year=2021,\n", " tbl_prof_type=\"Detailed\",\n", " table_id=\"C16001\",\n", ")" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# Rename columns to human-readable\n", "cols = {\n", " \"C16001_001E\": \"pop_5_years_over\",\n", " \"C16001_002E\": \"pop_english_only\",\n", " \"C16001_003E\": \"spanish_speaking_total\",\n", " \"C16001_004E\": \"spanish_english_vw\",\n", " \"C16001_005E\": \"spanish_english_lvw\",\n", " \"C16001_006E\": \"french_speaking_total\",\n", " \"C16001_007E\": \"french_english_vw\",\n", " \"C16001_008E\": \"french_english_lvw\",\n", " \"C16001_009E\": \"german_speaking_total\",\n", " \"C16001_010E\": \"german_english_vw\",\n", " \"C16001_011E\": \"german_english_lvw\",\n", " \"C16001_012E\": \"russian_speaking_total\",\n", " \"C16001_013E\": \"russian_english_vw\",\n", " \"C16001_014E\": \"russian_english_lvw\",\n", " \"C16001_015E\": \"other_indo_speaking_total\",\n", " \"C16001_016E\": \"other_indo_english_vw\",\n", " \"C16001_017E\": \"other_indo_english_lvw\",\n", " \"C16001_018E\": \"korean_speaking_total\",\n", " \"C16001_019E\": \"korean_english_vw\",\n", " \"C16001_020E\": \"korean_english_lvw\",\n", " \"C16001_021E\": \"chinese_speaking_total\",\n", " \"C16001_022E\": \"chinese_english_vw\",\n", " \"C16001_023E\": \"chinese_english_lvw\",\n", " \"C16001_024E\": \"vietnamese_speaking_total\",\n", " \"C16001_025E\": \"vietnamese_english_vw\",\n", " \"C16001_026E\": \"vietnamese_english_lvw\",\n", " \"C16001_027E\": \"tagalog_speaking_total\",\n", " \"C16001_028E\": \"tagalog_english_vw\",\n", " \"C16001_029E\": \"tagalog_english_lvw\",\n", " \"C16001_030E\": \"other_asian_speaking_total\",\n", " \"C16001_031E\": \"other_asian_english_vw\",\n", " \"C16001_032E\": \"other_asian_english_lvw\",\n", " \"C16001_033E\": \"arabic_speaking_total\",\n", " \"C16001_034E\": \"arabic_english_vw\",\n", " \"C16001_035E\": \"arabic_english_lvw\",\n", " \"C16001_036E\": \"other_unspec_speaking_total\",\n", " \"C16001_037E\": \"other_unspec_english_vw\",\n", " \"C16001_038E\": \"other_unspec_english_lvw\",\n", "}\n", "lep_df.rename(columns=cols, inplace=True)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
language_grouptotal_population
0pop_5_years_over7322155
1pop_english_only4200802
2spanish_english_lvw463480
8chinese_english_lvw297553
9vietnamese_english_lvw103317
6other_indo_english_lvw81386
10tagalog_english_lvw80000
11other_asian_english_lvw61642
7korean_english_lvw27806
5russian_english_lvw24751
13other_unspec_english_lvw15123
12arabic_english_lvw10003
3french_english_lvw5488
4german_english_lvw2759
\n", "
" ], "text/plain": [ " language_group total_population\n", "0 pop_5_years_over 7322155\n", "1 pop_english_only 4200802\n", "2 spanish_english_lvw 463480\n", "8 chinese_english_lvw 297553\n", "9 vietnamese_english_lvw 103317\n", "6 other_indo_english_lvw 81386\n", "10 tagalog_english_lvw 80000\n", "11 other_asian_english_lvw 61642\n", "7 korean_english_lvw 27806\n", "5 russian_english_lvw 24751\n", "13 other_unspec_english_lvw 15123\n", "12 arabic_english_lvw 10003\n", "3 french_english_lvw 5488\n", "4 german_english_lvw 2759" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum_cols = [\n", " \"pop_5_years_over\",\n", " \"pop_english_only\",\n", " \"spanish_english_lvw\",\n", " \"french_english_lvw\",\n", " \"german_english_lvw\",\n", " \"russian_english_lvw\",\n", " \"other_indo_english_lvw\",\n", " \"korean_english_lvw\",\n", " \"chinese_english_lvw\",\n", " \"vietnamese_english_lvw\",\n", " \"tagalog_english_lvw\",\n", " \"other_asian_english_lvw\",\n", " \"arabic_english_lvw\",\n", " \"other_unspec_english_lvw\",\n", "]\n", "lep_df[sum_cols].agg(\"sum\").reset_index().rename(\n", " columns={\"index\": \"language_group\", 0: \"total_population\"}\n", ").sort_values(\"total_population\", ascending=False)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "tracts_gdf = census.pull_census_tracts_geodata(year=2020, cartographic=True)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Merge tract geodata and census attributes" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "lep_gdf = pd.merge(tracts_gdf, lep_df, on=\"tract_geoid\")" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "work_dir = \"/Users/jcroff/Library/CloudStorage/Box-Box/DataViz Projects/Adhoc-Spatial-Analysis/Air District Language Access Plan Maps\"\n", "lep_gdf.to_file(\n", " os.path.join(work_dir, \"Data\", \"language_spoken_at_home_acs2017_2021.geojson\"),\n", " driver=\"GeoJSON\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "geo_env", "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.10.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }