Reputation: 522
I have a geopandas dataframe with a geom field that retains heterogeneous shapely data types. I want to group them by type (e.g. shapely.geometry.Point, shapely.geometry.Polygon, shapely.geometry.MultiPolygon, etc.). This task would be accomplished via something like this:
main_gdf_clean['geometry'].groupby(by=shapely.geometry.Point)
Is there a workable way to accomplish what I am describing?
Upvotes: 1
Views: 4121
Reputation: 59579
GeoSeries.geom_type
groupby the geometry type (df['geometry'].geom_type
) instead of the actual geometry.
import geopandas as gpd
from shapely.geometry import Polygon, Point
df = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)]),
Point(1,2), Point(2,3), Point(3,3)]).to_frame('geometry')
df = df.set_geometry(col='geometry') # So this example knows geometry
# Broadcast # of elements per unique shape type
df['N_items'] = df.groupby(df['geometry'].geom_type).transform('count')
# geometry N_items
#0 POLYGON ((1.00000 1.00000, 3.00000 1.00000, 3.... 2
#1 POLYGON ((3.00000 3.00000, 5.00000 3.00000, 5.... 2
#2 POINT (1.00000 2.00000) 3
#3 POINT (2.00000 3.00000) 3
#4 POINT (3.00000 3.00000) 3
Upvotes: 2