I want to count how many points there are per Polygon
# Credits of this code go to: https://stackoverflow.com/questions/69642668/the-indices-of-the-two-geoseries-are-different-understanding-indices/69644010#69644010
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
Now I sjoin
the two. I use df_poly
first, in order to add the points dfp
to the GeoDataframe
df_poly
.
df_poly.sjoin(dfp)
Now I want to count how many points
there are per polygon
.
I thought
df_poly.sjoin(dfp).groupby('OBJECTID').count()
But that does not add a column
to the GeoDataframe
df_poly
with the count
of each group
.