Get startedGet started for free

Spatially joining and getting counts

You will continue preparing your dataset for plotting a geopandas choropleth by creating a GeoDataFrame of the building permits spatially joined to the council districts. After that, you will be able to get counts of the building permits issued in each council district.

This exercise is part of the course

Visualizing Geospatial Data in Python

View Course

Exercise instructions

  • Create permits_geo from permits, the council_districts.crs, and the geometry in permits.
  • Spatially join permits_geo and the council_districts to get building permits within each council district. Call this permits_by_district.
  • Count permits in each district, permit_counts, by chaining groupby() and size() methods.
  • Create counts_df from permit_counts. Reset the index, and name the columns district and bldg_permits.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create permits_geo
permits_geo = gpd.GeoDataFrame(____, crs = ____.____, geometry = ____.____)

# Spatially join permits_geo and council_districts
permits_by_district = gpd.sjoin(____, ____, ____ = ____)
print(permits_by_district.head(2))

# Count permits in each district
permit_counts = permits_by_district.____(____).____()

# Convert permit_counts to a df with 2 columns: district and bldg_permits
counts_df = ____.to_frame()
counts_df = counts_df.____()
counts_df.____ = ['district', 'bldg_permits']
print(counts_df.head(2))
Edit and Run Code