geopandas.GeoDataFrame.dissolve¶
- GeoDataFrame.dissolve(by=None, aggfunc='first', as_index=True, level=None, sort=True, observed=False, dropna=True)¶
Dissolve geometries within groupby into single observation. This is accomplished by applying the unary_union method to all geometries within a groupself.
Observations associated with each groupby group will be aggregated using the aggfunc.
- Parameters
- bystring, default None
Column whose values define groups to be dissolved. If None, whole GeoDataFrame is considered a single group.
- aggfuncfunction or string, default “first”
Aggregation function for manipulation of data associated with each group. Passed to pandas groupby.agg method.
- as_indexboolean, default True
If true, groupby columns become index of result.
- levelint or str or sequence of int or sequence of str, default None
If the axis is a MultiIndex (hierarchical), group by a particular level or levels.
New in version 0.9.0.
- sortbool, default True
Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group.
New in version 0.9.0.
- observedbool, default False
This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.
New in version 0.9.0.
- dropnabool, default True
If True, and if group keys contain NA values, NA values together with row/column will be dropped. If False, NA values will also be treated as the key in groups.
This parameter is not supported for pandas < 1.1.0. A warning will be emitted for earlier pandas versions if a non-default value is given for this parameter.
New in version 0.9.0.
- Returns
- GeoDataFrame
See also
GeoDataFrame.explode
explode muti-part geometries into single geometries
Examples
>>> from shapely.geometry import Point >>> d = { ... "col1": ["name1", "name2", "name1"], ... "geometry": [Point(1, 2), Point(2, 1), Point(0, 1)], ... } >>> gdf = geopandas.GeoDataFrame(d, crs=4326) >>> gdf col1 geometry 0 name1 POINT (1.00000 2.00000) 1 name2 POINT (2.00000 1.00000) 2 name1 POINT (0.00000 1.00000)
>>> dissolved = gdf.dissolve('col1') >>> dissolved geometry col1 name1 MULTIPOINT (0.00000 1.00000, 1.00000 2.00000) name2 POINT (2.00000 1.00000)