{
"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",
" fips | \n",
" county | \n",
" state | \n",
" income-2015 | \n",
" income-1989a | \n",
" income-1989b | \n",
" change | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 00000 | \n",
" US | \n",
" US | \n",
" 55775.0 | \n",
" 28906 | \n",
" 53367.28102 | \n",
" 4.316843 | \n",
"
\n",
" \n",
" 1 | \n",
" 01000 | \n",
" Alabama | \n",
" AL | \n",
" 44833.0 | \n",
" 22202 | \n",
" 40990.11877 | \n",
" 8.571546 | \n",
"
\n",
" \n",
" 2 | \n",
" 01001 | \n",
" Autauga County | \n",
" AL | \n",
" 56580.0 | \n",
" 26898 | \n",
" 49660.04030 | \n",
" 12.230399 | \n",
"
\n",
" \n",
" 3 | \n",
" 01003 | \n",
" Baldwin County | \n",
" AL | \n",
" 52387.0 | \n",
" 24043 | \n",
" 44389.03818 | \n",
" 15.267074 | \n",
"
\n",
" \n",
" 4 | \n",
" 01005 | \n",
" Barbour County | \n",
" AL | \n",
" 31433.0 | \n",
" 18673 | \n",
" 34474.75398 | \n",
" -9.676945 | \n",
"
\n",
" \n",
"
\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",
" geometry | \n",
" name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" POLYGON ((-87.35930 35.00118, -85.60667 34.984... | \n",
" Alabama | \n",
"
\n",
" \n",
" 1 | \n",
" MULTIPOLYGON (((-131.60202 55.11798, -131.5691... | \n",
" Alaska | \n",
"
\n",
" \n",
" 2 | \n",
" POLYGON ((-109.04250 37.00026, -109.04798 31.3... | \n",
" Arizona | \n",
"
\n",
" \n",
" 3 | \n",
" POLYGON ((-94.47384 36.50186, -90.15254 36.496... | \n",
" Arkansas | \n",
"
\n",
" \n",
" 4 | \n",
" POLYGON ((-123.23326 42.00619, -122.37885 42.0... | \n",
" California | \n",
"
\n",
" \n",
"
\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",
" name | \n",
" alpha-2 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" AK | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" AZ | \n",
"
\n",
" \n",
"
\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",
" geometry | \n",
" name | \n",
" alpha-2 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" POLYGON ((-87.35930 35.00118, -85.60667 34.984... | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" MULTIPOLYGON (((-131.60202 55.11798, -131.5691... | \n",
" Alaska | \n",
" AK | \n",
"
\n",
" \n",
" 2 | \n",
" POLYGON ((-109.04250 37.00026, -109.04798 31.3... | \n",
" Arizona | \n",
" AZ | \n",
"
\n",
" \n",
" 3 | \n",
" POLYGON ((-94.47384 36.50186, -90.15254 36.496... | \n",
" Arkansas | \n",
" AR | \n",
"
\n",
" \n",
" 4 | \n",
" POLYGON ((-123.23326 42.00619, -120.00186 41.9... | \n",
" California | \n",
" CA | \n",
"
\n",
" \n",
"
\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",
" income-2015 | \n",
"
\n",
" \n",
" state | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AK | \n",
" 62561.5 | \n",
"
\n",
" \n",
" AL | \n",
" 38721.5 | \n",
"
\n",
" \n",
" AR | \n",
" 37890.0 | \n",
"
\n",
" \n",
" AZ | \n",
" 43810.0 | \n",
"
\n",
" \n",
" CA | \n",
" 53341.0 | \n",
"
\n",
" \n",
"
\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",
" geometry | \n",
" name | \n",
" alpha-2 | \n",
" medianincome | \n",
" change | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" POLYGON ((-87.35930 35.00118, -85.60667 34.984... | \n",
" Alabama | \n",
" AL | \n",
" 38721.5 | \n",
" 2.779114 | \n",
"
\n",
" \n",
" 1 | \n",
" MULTIPOLYGON (((-131.60202 55.11798, -131.5691... | \n",
" Alaska | \n",
" AK | \n",
" 62561.5 | \n",
" 14.758367 | \n",
"
\n",
" \n",
" 2 | \n",
" POLYGON ((-109.04250 37.00026, -109.04798 31.3... | \n",
" Arizona | \n",
" AZ | \n",
" 43810.0 | \n",
" NaN | \n",
"
\n",
" \n",
" 3 | \n",
" POLYGON ((-94.47384 36.50186, -90.15254 36.496... | \n",
" Arkansas | \n",
" AR | \n",
" 37890.0 | \n",
" 10.897394 | \n",
"
\n",
" \n",
" 4 | \n",
" POLYGON ((-123.23326 42.00619, -120.00186 41.9... | \n",
" California | \n",
" CA | \n",
" 53341.0 | \n",
" 6.716596 | \n",
"
\n",
" \n",
"
\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
}