Finding counts from a spatial join
You will be using a dataset of the building permits issued in Nashville during 2017. This DataFrame, called permits
, has been pre-loaded for you, along with the council_districts
GeoDataFrame.
This exercise is part of the course
Visualizing Geospatial Data in Python
Exercise instructions
- Create a
geometry
column inpermits
fromlat
andlng
. - Create
permits_geo
, a GeoDataFrame, usingpermits
, the crs fromcouncil_districts
, and thegeometry
frompermits
. - Use a spatial join to find permits issued
within
each council district. Print the first 2 rows. - Create
permit_counts
to show the count of permits within each district, usinggroupby()
and.size()
. Printpermit_counts
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import geopandas as gpd
# Create a geometry column in permits from lat and lng
permits[____] = gpd.points_from_xy(____.____ , ____.____)
# Build a GeoDataFrame: permits_geo
permits_geo = gpd.GeoDataFrame(____, crs = ____.crs, geometry = ____.geometry)
# Spatial join of permits_geo and council_districts
permits_by_district = gpd.____(permits_geo, council_districts, predicate = ____)
print(permits_by_district.head(2))
# Create permit_counts
permit_counts = permits_by_district.groupby([____]).____()
print(permit_counts)