[英]groupby shapely type in geopandas
I have a geopandas dataframe with a geom field that retains heterogeneous shapely data types.我有一个带有 geom 字段的 geopandas 数据框,该字段保留了异构的匀称数据类型。 I want to group them by type (eg shapely.geometry.Point, shapely.geometry.Polygon, shapely.geometry.MultiPolygon, etc.).我想按类型对它们进行分组(例如 shapely.geometry.Point、shapely.geometry.Polygon、shapely.geometry.MultiPolygon 等)。 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?有没有可行的方法来完成我所描述的事情?
GeoSeries.geom_type
groupby the geometry type ( df['geometry'].geom_type
) instead of the actual geometry. groupby 几何类型( df['geometry'].geom_type
)而不是实际几何。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.