Get startedGet started for free

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

View Course

Exercise instructions

  • Create a geometry column in permits from lat and lng.
  • Create permits_geo, a GeoDataFrame, using permits, the crs from council_districts, and the geometry from permits.
  • 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, using groupby() and .size(). Print permit_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)
Edit and Run Code