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.
Latihan ini adalah bagian dari kursus
Visualizing Geospatial Data in Python
Petunjuk latihan
- Create
permits_geofrompermits, thecouncil_districts.crs, and thegeometryinpermits. - Spatially join
permits_geoand thecouncil_districtsto get building permitswithineach council district. Call thispermits_by_district. - Count permits in each district,
permit_counts, by chaininggroupby()andsize()methods. - Create
counts_dffrompermit_counts. Reset the index, and name the columnsdistrictandbldg_permits.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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))