geopandas.GeoDataFrame.dissolve#

GeoDataFrame.dissolve(by=None, aggfunc='first', as_index=True, level=None, sort=True, observed=False, dropna=True, **kwargs)[source]#

Dissolve geometries within groupby into single observation. This is accomplished by applying the union_all method to all geometries within a groupself.

Observations associated with each groupby group will be aggregated using the aggfunc.

Parameters:
bystr or list-like, default None

Column(s) whose values define the groups to be dissolved. If None, the entire GeoDataFrame is considered as a single group. If a list-like object is provided, the values in the list are treated as categorical labels, and polygons will be combined based on the equality of these categorical labels.

aggfuncfunction or string, default “first”

Aggregation function for manipulation of data associated with each group. Passed to pandas groupby.agg method. Accepted combinations are:

  • function

  • string function name

  • list of functions and/or function names, e.g. [np.sum, ‘mean’]

  • dict of axis labels -> functions, function names or list of such.

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.

New in version 0.9.0.

**kwargs

Keyword arguments to be passed to the pandas DataFrameGroupby.agg method which is used by dissolve. In particular, numeric_only may be supplied, which will be required in pandas 2.0 for certain aggfuncs.

New in version 0.13.0.

Returns
——-
GeoDataFrame

See also

GeoDataFrame.explode

explode multi-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 2)
1  name2  POINT (2 1)
2  name1  POINT (0 1)
>>> dissolved = gdf.dissolve('col1')
>>> dissolved  
                        geometry
col1
name1  MULTIPOINT ((0 1), (1 2))
name2                POINT (2 1)