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
Exercise instructions
- Create
permits_geo
frompermits
, thecouncil_districts.crs
, and thegeometry
inpermits
. - Spatially join
permits_geo
and thecouncil_districts
to get building permitswithin
each council district. Call thispermits_by_district
. - Count permits in each district,
permit_counts
, by chaininggroupby()
andsize()
methods. - Create
counts_df
frompermit_counts
. Reset the index, and name the columnsdistrict
andbldg_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))