{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "\n", "income = pd.read_csv(\n", " \"https://raw.githubusercontent.com/pri-data/50-states/master/data/income-counties-states-national.csv\",\n", " dtype={\"fips\": str},\n", ")\n", "income[\"income-2015\"] = pd.to_numeric(income[\"income-2015\"], errors=\"coerce\")" ] }, { "cell_type": "code", "execution_count": 2, "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", "
fipscountystateincome-2015income-1989aincome-1989bchange
000000USUS55775.02890653367.281024.316843
101000AlabamaAL44833.02220240990.118778.571546
201001Autauga CountyAL56580.02689849660.0403012.230399
301003Baldwin CountyAL52387.02404344389.0381815.267074
401005Barbour CountyAL31433.01867334474.75398-9.676945
\n", "
" ], "text/plain": [ " fips county state income-2015 income-1989a income-1989b \\\n", "0 00000 US US 55775.0 28906 53367.28102 \n", "1 01000 Alabama AL 44833.0 22202 40990.11877 \n", "2 01001 Autauga County AL 56580.0 26898 49660.04030 \n", "3 01003 Baldwin County AL 52387.0 24043 44389.03818 \n", "4 01005 Barbour County AL 31433.0 18673 34474.75398 \n", "\n", " change \n", "0 4.316843 \n", "1 8.571546 \n", "2 12.230399 \n", "3 15.267074 \n", "4 -9.676945 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "income.head()" ] }, { "cell_type": "code", "execution_count": 3, "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", "
geometryname
0POLYGON ((-87.35930 35.00118, -85.60667 34.984...Alabama
1MULTIPOLYGON (((-131.60202 55.11798, -131.5691...Alaska
2POLYGON ((-109.04250 37.00026, -109.04798 31.3...Arizona
3POLYGON ((-94.47384 36.50186, -90.15254 36.496...Arkansas
4POLYGON ((-123.23326 42.00619, -122.37885 42.0...California
\n", "
" ], "text/plain": [ " geometry name\n", "0 POLYGON ((-87.35930 35.00118, -85.60667 34.984... Alabama\n", "1 MULTIPOLYGON (((-131.60202 55.11798, -131.5691... Alaska\n", "2 POLYGON ((-109.04250 37.00026, -109.04798 31.3... Arizona\n", "3 POLYGON ((-94.47384 36.50186, -90.15254 36.496... Arkansas\n", "4 POLYGON ((-123.23326 42.00619, -122.37885 42.0... California" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "import geopandas\n", "\n", "\n", "response = requests.get(\n", " \"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json\"\n", ")\n", "data = response.json()\n", "states = geopandas.GeoDataFrame.from_features(data, crs=\"EPSG:4326\")\n", "\n", "states.head()" ] }, { "cell_type": "code", "execution_count": 4, "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", "
namealpha-2
0AlabamaAL
1AlaskaAK
2ArizonaAZ
\n", "
" ], "text/plain": [ " name alpha-2\n", "0 Alabama AL\n", "1 Alaska AK\n", "2 Arizona AZ" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = requests.get(\n", " \"https://gist.githubusercontent.com/tvpmb/4734703/raw/\"\n", " \"b54d03154c339ed3047c66fefcece4727dfc931a/US%2520State%2520List\"\n", ")\n", "abbrs = pd.read_json(response.text)\n", "\n", "abbrs.head(3)" ] }, { "cell_type": "code", "execution_count": 5, "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", "
geometrynamealpha-2
0POLYGON ((-87.35930 35.00118, -85.60667 34.984...AlabamaAL
1MULTIPOLYGON (((-131.60202 55.11798, -131.5691...AlaskaAK
2POLYGON ((-109.04250 37.00026, -109.04798 31.3...ArizonaAZ
3POLYGON ((-94.47384 36.50186, -90.15254 36.496...ArkansasAR
4POLYGON ((-123.23326 42.00619, -120.00186 41.9...CaliforniaCA
\n", "
" ], "text/plain": [ " geometry name alpha-2\n", "0 POLYGON ((-87.35930 35.00118, -85.60667 34.984... Alabama AL\n", "1 MULTIPOLYGON (((-131.60202 55.11798, -131.5691... Alaska AK\n", "2 POLYGON ((-109.04250 37.00026, -109.04798 31.3... Arizona AZ\n", "3 POLYGON ((-94.47384 36.50186, -90.15254 36.496... Arkansas AR\n", "4 POLYGON ((-123.23326 42.00619, -120.00186 41.9... California CA" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statesmerge = states.merge(abbrs, how=\"left\", left_on=\"name\", right_on=\"name\")\n", "statesmerge[\"geometry\"] = statesmerge.geometry.simplify(0.05)\n", "\n", "statesmerge.head()" ] }, { "cell_type": "code", "execution_count": 6, "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", "
income-2015
state
AK62561.5
AL38721.5
AR37890.0
AZ43810.0
CA53341.0
\n", "
" ], "text/plain": [ " income-2015\n", "state \n", "AK 62561.5\n", "AL 38721.5\n", "AR 37890.0\n", "AZ 43810.0\n", "CA 53341.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "income.groupby(by=\"state\")[[\"state\", \"income-2015\"]].median().head()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "statesmerge[\"medianincome\"] = statesmerge.merge(\n", " income.groupby(by=\"state\")[[\"state\", \"income-2015\"]].median(),\n", " how=\"left\",\n", " left_on=\"alpha-2\",\n", " right_on=\"state\",\n", ")[\"income-2015\"]\n", "statesmerge[\"change\"] = statesmerge.merge(\n", " income.groupby(by=\"state\")[[\"state\", \"change\"]].median(),\n", " how=\"left\",\n", " left_on=\"alpha-2\",\n", " right_on=\"state\",\n", ")[\"change\"]" ] }, { "cell_type": "code", "execution_count": 8, "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", "
geometrynamealpha-2medianincomechange
0POLYGON ((-87.35930 35.00118, -85.60667 34.984...AlabamaAL38721.52.779114
1MULTIPOLYGON (((-131.60202 55.11798, -131.5691...AlaskaAK62561.514.758367
2POLYGON ((-109.04250 37.00026, -109.04798 31.3...ArizonaAZ43810.0NaN
3POLYGON ((-94.47384 36.50186, -90.15254 36.496...ArkansasAR37890.010.897394
4POLYGON ((-123.23326 42.00619, -120.00186 41.9...CaliforniaCA53341.06.716596
\n", "
" ], "text/plain": [ " geometry name alpha-2 \\\n", "0 POLYGON ((-87.35930 35.00118, -85.60667 34.984... Alabama AL \n", "1 MULTIPOLYGON (((-131.60202 55.11798, -131.5691... Alaska AK \n", "2 POLYGON ((-109.04250 37.00026, -109.04798 31.3... Arizona AZ \n", "3 POLYGON ((-94.47384 36.50186, -90.15254 36.496... Arkansas AR \n", "4 POLYGON ((-123.23326 42.00619, -120.00186 41.9... California CA \n", "\n", " medianincome change \n", "0 38721.5 2.779114 \n", "1 62561.5 14.758367 \n", "2 43810.0 NaN \n", "3 37890.0 10.897394 \n", "4 53341.0 6.716596 " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statesmerge.head()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43969.375" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statesmerge[\"medianincome\"].quantile(0.25)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import branca\n", "\n", "\n", "colormap = branca.colormap.LinearColormap(\n", " vmin=statesmerge[\"change\"].quantile(0.0),\n", " vmax=statesmerge[\"change\"].quantile(1),\n", " colors=[\"red\", \"orange\", \"lightblue\", \"green\", \"darkgreen\"],\n", " caption=\"State Level Median County Household Income (%)\",\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "from folium.features import GeoJsonPopup, GeoJsonTooltip\n", "\n", "\n", "m = folium.Map(location=[35.3, -97.6], zoom_start=4)\n", "\n", "popup = GeoJsonPopup(\n", " fields=[\"name\", \"change\"],\n", " aliases=[\"State\", \"% Change\"],\n", " localize=True,\n", " labels=True,\n", " style=\"background-color: yellow;\",\n", ")\n", "\n", "tooltip = GeoJsonTooltip(\n", " fields=[\"name\", \"medianincome\", \"change\"],\n", " aliases=[\"State:\", \"2015 Median Income(USD):\", \"Median % Change:\"],\n", " localize=True,\n", " sticky=False,\n", " labels=True,\n", " style=\"\"\"\n", " background-color: #F0EFEF;\n", " border: 2px solid black;\n", " border-radius: 3px;\n", " box-shadow: 3px;\n", " \"\"\",\n", " max_width=800,\n", ")\n", "\n", "\n", "g = folium.GeoJson(\n", " statesmerge,\n", " style_function=lambda x: {\n", " \"fillColor\": colormap(x[\"properties\"][\"change\"])\n", " if x[\"properties\"][\"change\"] is not None\n", " else \"transparent\",\n", " \"color\": \"black\",\n", " \"fillOpacity\": 0.4,\n", " },\n", " tooltip=tooltip,\n", " popup=popup,\n", ").add_to(m)\n", "\n", "colormap.add_to(m)\n", "\n", "m" ] } ], "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.9.0" } }, "nbformat": 4, "nbformat_minor": 4 }