Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Visualizing Geospatial Data in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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 bewerken en uitvoeren