{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mapping & Geocoding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this lesson, we're going to learn how to analyze and visualize geographic data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geocoding with GeoPy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we're going to geocode data — aka get coordinates from addresses or place names — with the Python package [GeoPy](https://geopy.readthedocs.io/en/stable/#). GeoPy makes it easier to use a range of third-party [geocoding API services](https://geopy.readthedocs.io/en/stable/#), such as Google, Bing, ArcGIS, and OpenStreetMap.\n",
"\n",
"Though most of these services require an API key, Nominatim, which uses OpenStreetMap data, does not, which is why we're going to use it here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install GeoPy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install geopy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import Nominatim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From GeoPy's list of possible geocoding services, we're going to import Nominatim:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from geopy.geocoders import Nominatim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nominatim & OpenStreetMap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nominatim (which means \"name\" in Latin) uses [OpenStreetMap data](https://www.openstreetmap.org/relation/174979) to match addresses with geopgraphic coordinates. Though we don't need an API key to use Nominatim, we do need to create a unique [application name](https://operations.osmfoundation.org/policies/nominatim/). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we're initializing Nominatim as a variable called `geolocator`. Change the application name below to your own application name:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"geolocator = Nominatim(user_agent=\"YOUR NAME's mapping app\", timeout=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To geocode an address or location, we simply use the `.geocode()` function:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"location = geolocator.geocode(\"South Cayuga Street\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Location(South Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America, (42.4359281, -76.4988639, 0.0))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"location"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Google Geocoding API"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Google Geocoding API is superior to Nominatim, but it requires an API key and more set up. To enable the Google Geocoding API and get an API key, see [Get Started with Google Maps Platform](https://developers.google.com/maps/gmp-get-started) and [Get Started with Geocoding API](https://developers.google.com/maps/documentation/geocoding/start)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"#from geopy.geocoders import GoogleV3\n",
"#google_geolocator = GoogleV3(api_key=\"YOUR-API-KEY HERE\")\n",
"#google_geolocator.geocode(\"Cayuga Street\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get Address"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"South Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America\n"
]
}
],
"source": [
"print(location.address)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get Latitude and Longitude"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42.4359281 -76.4988639\n"
]
}
],
"source": [
"print(location.latitude, location.longitude)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get \"Importance\" Score"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Importance: 0.4\n"
]
}
],
"source": [
"print(f\"Importance: {location.raw['importance']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get Class and Type"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Class: highway \n",
"Type: residential\n"
]
}
],
"source": [
"print(f\"Class: {location.raw['class']} \\nType: {location.raw['type']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Get Multiple Possible Matches"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true,
"tags": [
"output_scroll"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"College Avenue, Pemberton Tract, Midland, Midland County, Texas, United States of America\n",
"31.99450735 -102.08441405117188\n",
"Importance: 0.35000000000000003\n",
"College Avenue, Illinois Prairie Path, Wheaton, DuPage County, Illinois, 60187, United States of America\n",
"41.8683333 -88.0902778\n",
"Importance: 0.34778803910650136\n",
"College Avenue, Grantham, Cumberland, Pennsylvania, 17027, United States of America\n",
"40.155326 -76.994622\n",
"Importance: 0.3\n",
"College Avenue, London, Southwestern Ontario, Ontario, N6A 3H1, Canada\n",
"42.9982465 -81.2561233\n",
"Importance: 0.3\n",
"College Avenue, Spring Valley Estates, Athens-Clarke County Unified Government, Athens-Clarke County, Georgia, 30334-1458, United States of America\n",
"33.9578193 -83.3752778\n",
"Importance: 0.3\n",
"College Avenue, Windsor, Southwestern Ontario, Ontario, N9C 2K7, Canada\n",
"42.298467 -83.0652786\n",
"Importance: 0.3\n",
"College Avenue, Uptown, Racine, Wisconsin, 53403, United States of America\n",
"42.7160925 -87.784789\n",
"Importance: 0.3\n",
"College Avenue, Morris, Stevens County, Minnesota, 56267, United States of America\n",
"45.5906183 -95.9045979\n",
"Importance: 0.3\n",
"College Avenue, Swarthmore, Delaware County, Pennsylvania, 19081, United States of America\n",
"39.9058529 -75.3474084\n",
"Importance: 0.3\n",
"College Avenue, South Woodstock, Woodstock, Oxford County, Southwestern Ontario, Ontario, N4S 2E3, Canada\n",
"43.1246427 -80.745315\n",
"Importance: 0.3\n"
]
}
],
"source": [
"possible_locations = geolocator.geocode(\"College Ave\", exactly_one=False)\n",
"\n",
"for location in possible_locations:\n",
" print(location.address)\n",
" print(location.latitude, location.longitude)\n",
" print(f\"Importance: {location.raw['importance']}\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America\n",
"42.4431445 -76.4852319\n",
"Importance: 0.41000000000000003\n"
]
}
],
"source": [
"location = geolocator.geocode(\"College Ave, Ithaca NY\")\n",
"\n",
"print(location.address)\n",
"print(location.latitude, location.longitude)\n",
"print(f\"Importance: {location.raw['importance']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geocode with Pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To geocode every location in a CSV file, we can use Pandas, make a Python function, and `.apply()` it to every row in the CSV file."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"pd.set_option(\"max_rows\", 400)\n",
"pd.set_option(\"max_colwidth\", 400)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{admonition} Pandas\n",
":class: pandasreview\n",
" Do you need a refresher or introduction to the Python data analysis library Pandas? Be sure to check out Pandas Basics (1-3) in this textbook!\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we make a function with `geolocator.geocode()` and ask it to return the address, lat/lon, and importance score:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"def find_location(row):\n",
" \n",
" place = row['place']\n",
" \n",
" location = geolocator.geocode(place)\n",
" \n",
" if location != None:\n",
" return location.address, location.latitude, location.longitude, location.raw['importance']\n",
" else:\n",
" return \"Not Found\", \"Not Found\", \"Not Found\", \"Not Found\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To start exploring, let's read in a CSV file with a list of places in and around Ithaca."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"ithaca_df = pd.read_csv(\"../data/ithaca-places.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" place | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" College Town Bagels | \n",
"
\n",
" \n",
" 1 | \n",
" Ithaca Falls | \n",
"
\n",
" \n",
" 2 | \n",
" Moosewood Restaurant | \n",
"
\n",
" \n",
" 3 | \n",
" Cascadilla Gorge Trail | \n",
"
\n",
" \n",
" 4 | \n",
" Phillips Hall | \n",
"
\n",
" \n",
" 5 | \n",
" Carriage House Cafe | \n",
"
\n",
" \n",
" 6 | \n",
" Olin Library | \n",
"
\n",
" \n",
" 7 | \n",
" Purity Ice Cream | \n",
"
\n",
" \n",
" 8 | \n",
" Buttermilk Falls State Park | \n",
"
\n",
" \n",
" 9 | \n",
" Libe Slope | \n",
"
\n",
" \n",
" 10 | \n",
" Mundy Wildflower Garden | \n",
"
\n",
" \n",
" 11 | \n",
" Gimme! Coffee | \n",
"
\n",
" \n",
" 12 | \n",
" Beebe Lake | \n",
"
\n",
" \n",
" 13 | \n",
" Ithaca Tompkins Airport | \n",
"
\n",
" \n",
" 14 | \n",
" Wharton Studio Inc. | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" place\n",
"0 College Town Bagels\n",
"1 Ithaca Falls\n",
"2 Moosewood Restaurant\n",
"3 Cascadilla Gorge Trail\n",
"4 Phillips Hall\n",
"5 Carriage House Cafe\n",
"6 Olin Library\n",
"7 Purity Ice Cream\n",
"8 Buttermilk Falls State Park\n",
"9 Libe Slope\n",
"10 Mundy Wildflower Garden\n",
"11 Gimme! Coffee\n",
"12 Beebe Lake\n",
"13 Ithaca Tompkins Airport\n",
"14 Wharton Studio Inc."
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ithaca_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's `.apply()` our function to this Pandas dataframe and see what results Nominatim's geocoding service spits out."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" place | \n",
" address | \n",
" lat | \n",
" lon | \n",
" importance | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" College Town Bagels | \n",
" Collegetown Bagels, College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America | \n",
" 42.4424 | \n",
" -76.4851 | \n",
" 0.35 | \n",
"
\n",
" \n",
" 1 | \n",
" Ithaca Falls | \n",
" Ithaca Falls, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America | \n",
" 42.4528 | \n",
" -76.4916 | \n",
" 0.48084 | \n",
"
\n",
" \n",
" 2 | \n",
" Moosewood Restaurant | \n",
" Moosewood Restaurant, North Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America | \n",
" 42.4406 | \n",
" -76.4986 | \n",
" 0.475949 | \n",
"
\n",
" \n",
" 3 | \n",
" Cascadilla Gorge Trail | \n",
" Cascadilla Gorge Trail, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America | \n",
" 42.4429 | \n",
" -76.4821 | \n",
" 0.375 | \n",
"
\n",
" \n",
" 4 | \n",
" Phillips Hall | \n",
" Phillips Hall, Oak Street, Oxford, Oxford Township, Butler County, Ohio, 45056, United States of America | \n",
" 39.5052 | \n",
" -84.7368 | \n",
" 0.201 | \n",
"
\n",
" \n",
" 5 | \n",
" Carriage House Cafe | \n",
" Carriage House Cafe, B4380, Atcham, Shropshire, West Midlands, England, SY5 6QQ, United Kingdom | \n",
" 52.6857 | \n",
" -2.66969 | \n",
" 0.301 | \n",
"
\n",
" \n",
" 6 | \n",
" Olin Library | \n",
" Olin Library, Holt Avenue, Winter Park, Orange County, Florida, 32789, United States of America | \n",
" 28.591 | \n",
" -81.3492 | \n",
" 0.201 | \n",
"
\n",
" \n",
" 7 | \n",
" Purity Ice Cream | \n",
" Purity Ice Cream, Cascadilla Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America | \n",
" 42.4445 | \n",
" -76.5089 | \n",
" 0.301 | \n",
"
\n",
" \n",
" 8 | \n",
" Buttermilk Falls State Park | \n",
" Buttermilk Falls State Park, South Hill, Ithaca Town, Tompkins County, New York, United States of America | \n",
" 42.4162 | \n",
" -76.5164 | \n",
" 0.771981 | \n",
"
\n",
" \n",
" 9 | \n",
" Libe Slope | \n",
" Libe Slope, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, United States of America | \n",
" 42.4484 | \n",
" -76.4866 | \n",
" 0.4 | \n",
"
\n",
" \n",
" 10 | \n",
" Mundy Wildflower Garden | \n",
" Mundy Wildflower Garden, Forest Home, Ithaca Town, Tompkins County, New York, United States of America | \n",
" 42.4505 | \n",
" -76.4692 | \n",
" 0.45 | \n",
"
\n",
" \n",
" 11 | \n",
" Gimme! Coffee | \n",
" Gimme! Coffee, West State Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America | \n",
" 42.4395 | \n",
" -76.5064 | \n",
" 0.201 | \n",
"
\n",
" \n",
" 12 | \n",
" Beebe Lake | \n",
" Beebe Lake, Forest Home Drive, Forest Home, Ithaca Town, Tompkins County, New York, 14853, United States of America | \n",
" 42.4512 | \n",
" -76.4757 | \n",
" 0.201 | \n",
"
\n",
" \n",
" 13 | \n",
" Ithaca Tompkins Airport | \n",
" Ithaca Tompkins Regional Airport, Brown Road, Lansing, Lansing Town, Tompkins County, New York, 14850, United States of America | \n",
" 42.491 | \n",
" -76.4587 | \n",
" 0.624671 | \n",
"
\n",
" \n",
" 14 | \n",
" Wharton Studio Inc. | \n",
" Not Found | \n",
" Not Found | \n",
" Not Found | \n",
" Not Found | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" place \\\n",
"0 College Town Bagels \n",
"1 Ithaca Falls \n",
"2 Moosewood Restaurant \n",
"3 Cascadilla Gorge Trail \n",
"4 Phillips Hall \n",
"5 Carriage House Cafe \n",
"6 Olin Library \n",
"7 Purity Ice Cream \n",
"8 Buttermilk Falls State Park \n",
"9 Libe Slope \n",
"10 Mundy Wildflower Garden \n",
"11 Gimme! Coffee \n",
"12 Beebe Lake \n",
"13 Ithaca Tompkins Airport \n",
"14 Wharton Studio Inc. \n",
"\n",
" address \\\n",
"0 Collegetown Bagels, College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n",
"1 Ithaca Falls, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n",
"2 Moosewood Restaurant, North Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n",
"3 Cascadilla Gorge Trail, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America \n",
"4 Phillips Hall, Oak Street, Oxford, Oxford Township, Butler County, Ohio, 45056, United States of America \n",
"5 Carriage House Cafe, B4380, Atcham, Shropshire, West Midlands, England, SY5 6QQ, United Kingdom \n",
"6 Olin Library, Holt Avenue, Winter Park, Orange County, Florida, 32789, United States of America \n",
"7 Purity Ice Cream, Cascadilla Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America \n",
"8 Buttermilk Falls State Park, South Hill, Ithaca Town, Tompkins County, New York, United States of America \n",
"9 Libe Slope, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, United States of America \n",
"10 Mundy Wildflower Garden, Forest Home, Ithaca Town, Tompkins County, New York, United States of America \n",
"11 Gimme! Coffee, West State Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America \n",
"12 Beebe Lake, Forest Home Drive, Forest Home, Ithaca Town, Tompkins County, New York, 14853, United States of America \n",
"13 Ithaca Tompkins Regional Airport, Brown Road, Lansing, Lansing Town, Tompkins County, New York, 14850, United States of America \n",
"14 Not Found \n",
"\n",
" lat lon importance \n",
"0 42.4424 -76.4851 0.35 \n",
"1 42.4528 -76.4916 0.48084 \n",
"2 42.4406 -76.4986 0.475949 \n",
"3 42.4429 -76.4821 0.375 \n",
"4 39.5052 -84.7368 0.201 \n",
"5 52.6857 -2.66969 0.301 \n",
"6 28.591 -81.3492 0.201 \n",
"7 42.4445 -76.5089 0.301 \n",
"8 42.4162 -76.5164 0.771981 \n",
"9 42.4484 -76.4866 0.4 \n",
"10 42.4505 -76.4692 0.45 \n",
"11 42.4395 -76.5064 0.201 \n",
"12 42.4512 -76.4757 0.201 \n",
"13 42.491 -76.4587 0.624671 \n",
"14 Not Found Not Found Not Found "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ithaca_df[['address', 'lat', 'lon', 'importance']] = ithaca_df.apply(find_location, axis=\"columns\", result_type=\"expand\")\n",
"ithaca_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**What do you notice about these results?** ☝️☝️☝️"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[Wharton Studio Inc.](https://exhibits.library.cornell.edu/biggest-little-fashion-city/feature/wharton-studio-inc)** (1914-1919) — early 20th-century Ithaca movie studio, located in what is now Stewart Park \n",
"*To check out more historical photos of Wharton Studio Inc., see [the Cornell library](https://digital.library.cornell.edu/catalog/ss:550440).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Making Interactive Maps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To map our geocoded coordinates, we're going to use the Python library [Folium](https://python-visualization.github.io/folium/). Folium is built on top of the popular JavaScript library [Leaflet](https://leafletjs.com/)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To install and import Folium, run the cells below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"output_scroll"
]
},
"outputs": [],
"source": [
"!pip install folium"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import folium"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Base Map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we need to establish a base map. This is where we'll map our geocoded Ithaca locations. To do so, we're going to call `folium.Map()`and enter the general latitude/longitude coordinates of the Ithaca area at a particular zoom.\n",
"\n",
"(To find latitude/longitude coordintes for a particular location, you can use Google Maps, [as described here](https://support.google.com/maps/answer/18539?co=GENIE.Platform%3DDesktop&hl=en).)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ithaca_map = folium.Map(location=[42.44, -76.5], zoom_start=14)\n",
"ithaca_map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add a Marker"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Adding a marker to a map is easy with Folium! We'll simply call `folium.Marker()` at a particular lat/lon, enter some text to display when the marker is clicked on, and then add it to our base map."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"folium.Marker(location=[42.444695, -76.482233], popup=\"Intro to Cultural Analytics\").add_to(ithaca_map)\n",
"ithaca_map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add Markers From Pandas Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To add markers for every location in our Pandas dataframe, we can make a Python function and `.apply()` it to every row in the dataframe."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def create_map_markers(row, map_name):\n",
" folium.Marker(location=[row['lat'], row['lon']], popup=row['place']).add_to(map_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we apply this function to our dataframe, we're going to drop any locations that were \"Not Found\" (which would cause `folium.Marker()` to return an error)."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"found_ithaca_locations = ithaca_df[ithaca_df['address'] != \"Not Found\"]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"found_ithaca_locations.apply(create_map_markers, map_name=ithaca_map, axis='columns')\n",
"ithaca_map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save Map"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"ithaca_map.save(\"Ithaca-map.html\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Torn Apart / Separados"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data in this section was drawn from [Torn Apart / Separados Project](https://github.com/xpmethod/torn-apart-open-data). It maps the locations of Immigration and Customs Enforcement (ICE) detention facilities, as featured in [Volume 1](http://xpmethod.plaintext.in/torn-apart/volume/1/)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add a Circle Marker"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a few [different kinds of markers](https://python-visualization.github.io/folium/quickstart.html#Markers) that we can add to a Folium map, including circles. To make a circle, we can call `folium.CircleMarker()` with a particular radius and the option to fill in the circle. You can explore more customization options in the [Folium documentation](https://python-visualization.github.io/folium/modules.html#folium.vector_layers.CircleMarker). We're also going to add a hover `tooltip` in addition to a `popup`."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"def create_ICE_map_markers(row, map_name):\n",
" \n",
" folium.CircleMarker(location=[row['lat'], row['lon']], raidus=100, fill=True,\n",
" popup=folium.Popup(f\"{row['Name'].title()}
{row['City'].title()}, {row['State']}\", max_width=200),\n",
" tooltip=f\"{row['Name'].title()}
{row['City'].title()}, {row['State']}\"\n",
" ).add_to(map_name)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"scrolled": true,
"tags": [
"output_scroll"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" lat | \n",
" lon | \n",
" adpSum | \n",
" onWeb | \n",
" Flags | \n",
" fulladdr | \n",
" DETLOC | \n",
" Name | \n",
" Address | \n",
" City | \n",
" ... | \n",
" ICE.Threat.Level.2 | \n",
" ICE.Threat.Level.3 | \n",
" No.ICE.Threat.Level | \n",
" Facility.Operator | \n",
" FY17.Calendar.Days.in.Use | \n",
" FY17...of.Days.in.Use | \n",
" FY17.Total.Mandays | \n",
" FY17.Max.Pop.Count | \n",
" geocodelat | \n",
" geocodelon | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 28.895000 | \n",
" -99.121200 | \n",
" 8391 | \n",
" 28.8950 | \n",
" NaN | \n",
" 566 VETERANS DRIVE PEARSALL TX 78061 | \n",
" STCDFTX | \n",
" SOUTH TEXAS DETENTION COMPLEX | \n",
" 566 VETERANS DRIVE | \n",
" PEARSALL | \n",
" ... | \n",
" 112 | \n",
" 311 | \n",
" 1187 | \n",
" GEO | \n",
" 372 | \n",
" 1.02 | \n",
" 598554 | \n",
" 1854 | \n",
" 28.896498 | \n",
" -99.116863 | \n",
"
\n",
" \n",
" 1 | \n",
" 32.036600 | \n",
" -84.771800 | \n",
" 8004 | \n",
" 32.0366 | \n",
" NaN | \n",
" 146 CCA ROAD LUMPKIN GA 31815 | \n",
" STWRTGA | \n",
" STEWART DETENTION CENTER | \n",
" 146 CCA ROAD | \n",
" LUMPKIN | \n",
" ... | \n",
" 344 | \n",
" 365 | \n",
" 671 | \n",
" CCA | \n",
" 372 | \n",
" 1.02 | \n",
" 671515 | \n",
" 1992 | \n",
" 32.037982 | \n",
" -84.772465 | \n",
"
\n",
" \n",
" 2 | \n",
" 34.559200 | \n",
" -117.441000 | \n",
" 7265 | \n",
" 34.5592 | \n",
" NaN | \n",
" 10250 RANCHO ROAD ADELANTO CA 92301 | \n",
" ADLNTCA | \n",
" ADELANTO ICE PROCESSING CENTER | \n",
" 10250 RANCHO ROAD | \n",
" ADELANTO | \n",
" ... | \n",
" 206 | \n",
" 164 | \n",
" 726 | \n",
" GEO | \n",
" 372 | \n",
" 1.02 | \n",
" 625414 | \n",
" 1918 | \n",
" 34.557721 | \n",
" -117.442524 | \n",
"
\n",
" \n",
" 3 | \n",
" 32.817700 | \n",
" -111.520000 | \n",
" 7096 | \n",
" 32.8177 | \n",
" NaN | \n",
" 1705 EAST HANNA RD. ELOY AZ 85131 | \n",
" EAZ | \n",
" ELOY FEDERAL CONTRACT FACILITY | \n",
" 1705 EAST HANNA RD. | \n",
" ELOY | \n",
" ... | \n",
" 154 | \n",
" 232 | \n",
" 785 | \n",
" CCA | \n",
" 372 | \n",
" 1.02 | \n",
" 502952 | \n",
" 1489 | \n",
" 32.821231 | \n",
" -111.549772 | \n",
"
\n",
" \n",
" 4 | \n",
" 47.249100 | \n",
" -122.421000 | \n",
" 6757 | \n",
" 47.2491 | \n",
" NaN | \n",
" 1623 E. J STREET TACOMA WA 98421 | \n",
" CSCNWWA | \n",
" NORTHWEST DETENTION CENTER | \n",
" 1623 E. J STREET | \n",
" TACOMA | \n",
" ... | \n",
" 166 | \n",
" 174 | \n",
" 693 | \n",
" GEO | \n",
" 372 | \n",
" 1.02 | \n",
" 519386 | \n",
" 1563 | \n",
" 47.250214 | \n",
" -122.422746 | \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",
" 481 | \n",
" 39.671492 | \n",
" -75.714329 | \n",
" 1 | \n",
" 0.0000 | \n",
" NaN | \n",
" 970 BROAD STREET NEWARK NJ 7102 | \n",
" NEWHOLD | \n",
" NEW/INS OS HOLD ROOM | \n",
" 970 BROAD STREET | \n",
" NEWARK | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" FEDERAL | \n",
" 37 | \n",
" 0.10 | \n",
" 50 | \n",
" 2 | \n",
" 39.671492 | \n",
" -75.714329 | \n",
"
\n",
" \n",
" 482 | \n",
" 26.204563 | \n",
" -98.270145 | \n",
" 1 | \n",
" 0.0000 | \n",
" NaN | \n",
" BENTSEN TOWER, 1701 W BUS HWY 83 MCALLEN TX 78501 | \n",
" USMS3TX | \n",
" US MARSHALS (SOUTH DISTRICT, TEXAS) | \n",
" BENTSEN TOWER, 1701 W BUS HWY 83 | \n",
" MCALLEN | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" FEDERAL | \n",
" 0 | \n",
" 0.00 | \n",
" 0 | \n",
" 0 | \n",
" 26.204563 | \n",
" -98.270145 | \n",
"
\n",
" \n",
" 483 | \n",
" 41.528728 | \n",
" -73.363545 | \n",
" 1 | \n",
" 0.0000 | \n",
" NaN | \n",
" BRIDGEWATER STATE HOSPITAL BRIDGEWATER MA 2324 | \n",
" MABSHOS | \n",
" BRIDGEWATER STATE HOSPITAL | \n",
" BRIDGEWATER STATE HOSPITAL | \n",
" BRIDGEWATER | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" HOSPITAL | \n",
" 0 | \n",
" 0.00 | \n",
" 0 | \n",
" 0 | \n",
" 41.528728 | \n",
" -73.363545 | \n",
"
\n",
" \n",
" 484 | \n",
" NaN | \n",
" NaN | \n",
" 1 | \n",
" 0.0000 | \n",
" NaN | \n",
" Redacted Redacted Redacted Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ORR | \n",
" 17 | \n",
" 0.05 | \n",
" 17 | \n",
" 1 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 485 | \n",
" NaN | \n",
" NaN | \n",
" 1 | \n",
" 0.0000 | \n",
" NaN | \n",
" Redacted Redacted Redacted Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" Redacted | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" NaN | \n",
" 0 | \n",
" 0.00 | \n",
" 0 | \n",
" 0 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
486 rows × 51 columns
\n",
"
"
],
"text/plain": [
" lat lon adpSum onWeb Flags \\\n",
"0 28.895000 -99.121200 8391 28.8950 NaN \n",
"1 32.036600 -84.771800 8004 32.0366 NaN \n",
"2 34.559200 -117.441000 7265 34.5592 NaN \n",
"3 32.817700 -111.520000 7096 32.8177 NaN \n",
"4 47.249100 -122.421000 6757 47.2491 NaN \n",
".. ... ... ... ... ... \n",
"481 39.671492 -75.714329 1 0.0000 NaN \n",
"482 26.204563 -98.270145 1 0.0000 NaN \n",
"483 41.528728 -73.363545 1 0.0000 NaN \n",
"484 NaN NaN 1 0.0000 NaN \n",
"485 NaN NaN 1 0.0000 NaN \n",
"\n",
" fulladdr DETLOC \\\n",
"0 566 VETERANS DRIVE PEARSALL TX 78061 STCDFTX \n",
"1 146 CCA ROAD LUMPKIN GA 31815 STWRTGA \n",
"2 10250 RANCHO ROAD ADELANTO CA 92301 ADLNTCA \n",
"3 1705 EAST HANNA RD. ELOY AZ 85131 EAZ \n",
"4 1623 E. J STREET TACOMA WA 98421 CSCNWWA \n",
".. ... ... \n",
"481 970 BROAD STREET NEWARK NJ 7102 NEWHOLD \n",
"482 BENTSEN TOWER, 1701 W BUS HWY 83 MCALLEN TX 78501 USMS3TX \n",
"483 BRIDGEWATER STATE HOSPITAL BRIDGEWATER MA 2324 MABSHOS \n",
"484 Redacted Redacted Redacted Redacted Redacted \n",
"485 Redacted Redacted Redacted Redacted Redacted \n",
"\n",
" Name Address \\\n",
"0 SOUTH TEXAS DETENTION COMPLEX 566 VETERANS DRIVE \n",
"1 STEWART DETENTION CENTER 146 CCA ROAD \n",
"2 ADELANTO ICE PROCESSING CENTER 10250 RANCHO ROAD \n",
"3 ELOY FEDERAL CONTRACT FACILITY 1705 EAST HANNA RD. \n",
"4 NORTHWEST DETENTION CENTER 1623 E. J STREET \n",
".. ... ... \n",
"481 NEW/INS OS HOLD ROOM 970 BROAD STREET \n",
"482 US MARSHALS (SOUTH DISTRICT, TEXAS) BENTSEN TOWER, 1701 W BUS HWY 83 \n",
"483 BRIDGEWATER STATE HOSPITAL BRIDGEWATER STATE HOSPITAL \n",
"484 Redacted Redacted \n",
"485 Redacted Redacted \n",
"\n",
" City ... ICE.Threat.Level.2 ICE.Threat.Level.3 \\\n",
"0 PEARSALL ... 112 311 \n",
"1 LUMPKIN ... 344 365 \n",
"2 ADELANTO ... 206 164 \n",
"3 ELOY ... 154 232 \n",
"4 TACOMA ... 166 174 \n",
".. ... ... ... ... \n",
"481 NEWARK ... 0 0 \n",
"482 MCALLEN ... 0 0 \n",
"483 BRIDGEWATER ... 0 0 \n",
"484 Redacted ... 0 0 \n",
"485 Redacted ... 0 0 \n",
"\n",
" No.ICE.Threat.Level Facility.Operator FY17.Calendar.Days.in.Use \\\n",
"0 1187 GEO 372 \n",
"1 671 CCA 372 \n",
"2 726 GEO 372 \n",
"3 785 CCA 372 \n",
"4 693 GEO 372 \n",
".. ... ... ... \n",
"481 1 FEDERAL 37 \n",
"482 0 FEDERAL 0 \n",
"483 0 HOSPITAL 0 \n",
"484 0 ORR 17 \n",
"485 0 NaN 0 \n",
"\n",
" FY17...of.Days.in.Use FY17.Total.Mandays FY17.Max.Pop.Count geocodelat \\\n",
"0 1.02 598554 1854 28.896498 \n",
"1 1.02 671515 1992 32.037982 \n",
"2 1.02 625414 1918 34.557721 \n",
"3 1.02 502952 1489 32.821231 \n",
"4 1.02 519386 1563 47.250214 \n",
".. ... ... ... ... \n",
"481 0.10 50 2 39.671492 \n",
"482 0.00 0 0 26.204563 \n",
"483 0.00 0 0 41.528728 \n",
"484 0.05 17 1 NaN \n",
"485 0.00 0 0 NaN \n",
"\n",
" geocodelon \n",
"0 -99.116863 \n",
"1 -84.772465 \n",
"2 -117.442524 \n",
"3 -111.549772 \n",
"4 -122.422746 \n",
".. ... \n",
"481 -75.714329 \n",
"482 -98.270145 \n",
"483 -73.363545 \n",
"484 NaN \n",
"485 NaN \n",
"\n",
"[486 rows x 51 columns]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ICE_df = pd.read_csv(\"../data/ICE-facilities.csv\")\n",
"ICE_df"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"US_map = folium.Map(location=[42, -102], zoom_start=4)\n",
"US_map"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"ICE_df = ICE_df.dropna(subset=['lat', 'lon'])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ICE_df.apply(create_ICE_map_markers, map_name=US_map, axis=\"columns\")\n",
"US_map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choropleth Maps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```margin Choropleth Map\n",
"Choropleth map = a map where areas are shaded according to a value\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data in this section was drawn from [Torn Apart / Separados Project](https://github.com/xpmethod/torn-apart-open-data). This data maps the \"cumulative ICE awards since 2014 to contractors by congressional district,\" as featured in [Volume 2](http://xpmethod.plaintext.in/torn-apart/volume/2/)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a chropleth map with Folium, we need to pair a \"geo.json\" file (which indicates which parts of the map to shade) with a CSV file (which includes the variable that we want to shade by)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following data was drawn from [the Torn Apart / Separados project](https://github.com/xpmethod/torn-apart/tree/master/data/districts)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"US_districts_geo_json = \"../data/ICE_money_districts.geo.json\""
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"US_districts_csv = pd.read_csv(\"../data/ICE_money_districts.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"US_districts_csv = US_districts_csv .dropna(subset=['districtName', 'representative'])"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" id2 | \n",
" state | \n",
" districtNumber | \n",
" districtName | \n",
" party | \n",
" district_url | \n",
" representative | \n",
" representative_photo_url | \n",
" total_awards | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 5001500US0101 | \n",
" 101 | \n",
" Alabama | \n",
" 1 | \n",
" ta-ordinal-st-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Alabama%27s_1st_congressional_district | \n",
" Bradley Byrne | \n",
" https://upload.wikimedia.org/wikipedia/commons/7/71/Rep_Bradley_Byrne_%28cropped%29.jpg | \n",
" 0.00 | \n",
"
\n",
" \n",
" 1 | \n",
" 5001500US0102 | \n",
" 102 | \n",
" Alabama | \n",
" 2 | \n",
" ta-ordinal-nd-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Alabama%27s_2nd_congressional_district | \n",
" Martha Roby | \n",
" https://upload.wikimedia.org/wikipedia/commons/5/55/Martha_roby_113_congressional_portrait_%28cropped%29.jpg | \n",
" 38577.40 | \n",
"
\n",
" \n",
" 2 | \n",
" 5001500US0103 | \n",
" 103 | \n",
" Alabama | \n",
" 3 | \n",
" ta-ordinal-rd-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Alabama%27s_3rd_congressional_district | \n",
" Mike Rogers | \n",
" https://upload.wikimedia.org/wikipedia/commons/e/ee/Mike_Rogers_official_photo_%28cropped%29.jpg | \n",
" 0.00 | \n",
"
\n",
" \n",
" 3 | \n",
" 5001500US0104 | \n",
" 104 | \n",
" Alabama | \n",
" 4 | \n",
" ta-ordinal-th-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Alabama%27s_4th_congressional_district | \n",
" Robert Aderholt | \n",
" https://upload.wikimedia.org/wikipedia/commons/9/9f/Rep._Robert_B._Aderholt_%28cropped%29.jpg | \n",
" 171873.55 | \n",
"
\n",
" \n",
" 4 | \n",
" 5001500US0105 | \n",
" 105 | \n",
" Alabama | \n",
" 5 | \n",
" ta-ordinal-th-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Alabama%27s_5th_congressional_district | \n",
" Mo Brooks | \n",
" https://upload.wikimedia.org/wikipedia/commons/b/b6/Mo_Brooks_Portrait_%28cropped%29.jpg | \n",
" 40346.00 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 432 | \n",
" 5001500US5506 | \n",
" 5506 | \n",
" Wisconsin | \n",
" 6 | \n",
" ta-ordinal-th-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Wisconsin%27s_6th_congressional_district | \n",
" Glenn Grothman | \n",
" https://upload.wikimedia.org/wikipedia/commons/1/16/Glenn_Grothman_official_congressional_photo_%28cropped%29.jpg | \n",
" 3242401.61 | \n",
"
\n",
" \n",
" 433 | \n",
" 5001500US5507 | \n",
" 5507 | \n",
" Wisconsin | \n",
" 7 | \n",
" ta-ordinal-th-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Wisconsin%27s_7th_congressional_district | \n",
" Sean Duffy | \n",
" https://upload.wikimedia.org/wikipedia/commons/d/d7/Sean_Duffy_official_congressional_photo_%28cropped%29.jpg | \n",
" 32698.55 | \n",
"
\n",
" \n",
" 434 | \n",
" 5001500US5508 | \n",
" 5508 | \n",
" Wisconsin | \n",
" 8 | \n",
" ta-ordinal-th-m | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Wisconsin%27s_8th_congressional_district | \n",
" Mike Gallagher | \n",
" https://upload.wikimedia.org/wikipedia/commons/a/ad/Mike_Gallagher_Official_Portrait_2017_%28cropped%29.png | \n",
" 237392.73 | \n",
"
\n",
" \n",
" 435 | \n",
" 5001500US5600 | \n",
" 5600 | \n",
" Wyoming | \n",
" 0 | \n",
" ta-at-large-district | \n",
" republican | \n",
" https://en.wikipedia.org/wiki/Wyoming%27s_at-large_congressional_district | \n",
" Liz Cheney | \n",
" https://upload.wikimedia.org/wikipedia/commons/d/dd/Liz_Cheney_official_portrait.jpg | \n",
" 0.00 | \n",
"
\n",
" \n",
" 436 | \n",
" 5001500US7298 | \n",
" 7298 | \n",
" Puerto Rico | \n",
" 98 | \n",
" ta-ordinal-th-m | \n",
" no-rep | \n",
" https://en.wikipedia.org/wiki/Puerto_Rico%27s_at-large_congressional_district | \n",
" Resident Commissioner Jenniffer González | \n",
" https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg/800px-Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg | \n",
" 323102.10 | \n",
"
\n",
" \n",
"
\n",
"
431 rows × 10 columns
\n",
"
"
],
"text/plain": [
" id id2 state districtNumber districtName \\\n",
"0 5001500US0101 101 Alabama 1 ta-ordinal-st-m \n",
"1 5001500US0102 102 Alabama 2 ta-ordinal-nd-m \n",
"2 5001500US0103 103 Alabama 3 ta-ordinal-rd-m \n",
"3 5001500US0104 104 Alabama 4 ta-ordinal-th-m \n",
"4 5001500US0105 105 Alabama 5 ta-ordinal-th-m \n",
".. ... ... ... ... ... \n",
"432 5001500US5506 5506 Wisconsin 6 ta-ordinal-th-m \n",
"433 5001500US5507 5507 Wisconsin 7 ta-ordinal-th-m \n",
"434 5001500US5508 5508 Wisconsin 8 ta-ordinal-th-m \n",
"435 5001500US5600 5600 Wyoming 0 ta-at-large-district \n",
"436 5001500US7298 7298 Puerto Rico 98 ta-ordinal-th-m \n",
"\n",
" party \\\n",
"0 republican \n",
"1 republican \n",
"2 republican \n",
"3 republican \n",
"4 republican \n",
".. ... \n",
"432 republican \n",
"433 republican \n",
"434 republican \n",
"435 republican \n",
"436 no-rep \n",
"\n",
" district_url \\\n",
"0 https://en.wikipedia.org/wiki/Alabama%27s_1st_congressional_district \n",
"1 https://en.wikipedia.org/wiki/Alabama%27s_2nd_congressional_district \n",
"2 https://en.wikipedia.org/wiki/Alabama%27s_3rd_congressional_district \n",
"3 https://en.wikipedia.org/wiki/Alabama%27s_4th_congressional_district \n",
"4 https://en.wikipedia.org/wiki/Alabama%27s_5th_congressional_district \n",
".. ... \n",
"432 https://en.wikipedia.org/wiki/Wisconsin%27s_6th_congressional_district \n",
"433 https://en.wikipedia.org/wiki/Wisconsin%27s_7th_congressional_district \n",
"434 https://en.wikipedia.org/wiki/Wisconsin%27s_8th_congressional_district \n",
"435 https://en.wikipedia.org/wiki/Wyoming%27s_at-large_congressional_district \n",
"436 https://en.wikipedia.org/wiki/Puerto_Rico%27s_at-large_congressional_district \n",
"\n",
" representative \\\n",
"0 Bradley Byrne \n",
"1 Martha Roby \n",
"2 Mike Rogers \n",
"3 Robert Aderholt \n",
"4 Mo Brooks \n",
".. ... \n",
"432 Glenn Grothman \n",
"433 Sean Duffy \n",
"434 Mike Gallagher \n",
"435 Liz Cheney \n",
"436 Resident Commissioner Jenniffer González \n",
"\n",
" representative_photo_url \\\n",
"0 https://upload.wikimedia.org/wikipedia/commons/7/71/Rep_Bradley_Byrne_%28cropped%29.jpg \n",
"1 https://upload.wikimedia.org/wikipedia/commons/5/55/Martha_roby_113_congressional_portrait_%28cropped%29.jpg \n",
"2 https://upload.wikimedia.org/wikipedia/commons/e/ee/Mike_Rogers_official_photo_%28cropped%29.jpg \n",
"3 https://upload.wikimedia.org/wikipedia/commons/9/9f/Rep._Robert_B._Aderholt_%28cropped%29.jpg \n",
"4 https://upload.wikimedia.org/wikipedia/commons/b/b6/Mo_Brooks_Portrait_%28cropped%29.jpg \n",
".. ... \n",
"432 https://upload.wikimedia.org/wikipedia/commons/1/16/Glenn_Grothman_official_congressional_photo_%28cropped%29.jpg \n",
"433 https://upload.wikimedia.org/wikipedia/commons/d/d7/Sean_Duffy_official_congressional_photo_%28cropped%29.jpg \n",
"434 https://upload.wikimedia.org/wikipedia/commons/a/ad/Mike_Gallagher_Official_Portrait_2017_%28cropped%29.png \n",
"435 https://upload.wikimedia.org/wikipedia/commons/d/dd/Liz_Cheney_official_portrait.jpg \n",
"436 https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg/800px-Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg \n",
"\n",
" total_awards \n",
"0 0.00 \n",
"1 38577.40 \n",
"2 0.00 \n",
"3 171873.55 \n",
"4 40346.00 \n",
".. ... \n",
"432 3242401.61 \n",
"433 32698.55 \n",
"434 237392.73 \n",
"435 0.00 \n",
"436 323102.10 \n",
"\n",
"[431 rows x 10 columns]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"US_districts_csv"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"US_map = folium.Map(location=[42, -102], zoom_start=4)\n",
"\n",
"folium.Choropleth(\n",
" geo_data = US_districts_geo_json,\n",
" name = 'choropleth',\n",
" data = US_districts_csv,\n",
" columns = ['districtName', 'total_awards'],\n",
" key_on = 'feature.properties.districtName',\n",
" fill_color = 'GnBu',\n",
" line_opacity = 0.2,\n",
" legend_name= 'Total ICE Money Received'\n",
").add_to(US_map)\n",
"\n",
"US_map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add a Tooltip to Choropleth"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tooltip = folium.features.GeoJson(\n",
" US_districts_geo_json,\n",
" tooltip=folium.features.GeoJsonTooltip(fields=['representative', 'state', 'party', 'total_value'], localize=True)\n",
" )\n",
"US_map.add_child(tooltip)\n",
"US_map"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}