LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Visualizing Geospatial Data in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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))
Code bearbeiten und ausführen