# Aggregation with dissolve¶

Spatial data are often more granular than we need. For example, we might have data on sub-national units, but we’re actually interested in studying patterns at the level of countries.

In a non-spatial setting, when all we need are summary statistics of the data, we aggregate our data using the `groupby` function. But for spatial data, we sometimes also need to aggregate geometric features. In the geopandas library, we can aggregate geometric features using the `dissolve` function.

`dissolve` can be thought of as doing three things: (a) it dissolves all the geometries within a given group together into a single geometric feature (using the `unary_union` method), and (b) it aggregates all the rows of data in a group using `groupby.aggregate()`, and (c) it combines those two results.

## `dissolve` Example¶

Suppose we are interested in studying continents, but we only have country-level data like the country dataset included in geopandas. We can easily convert this to a continent-level dataset.

First, let’s look at the most simple case where we just want continent shapes and names. By default, `dissolve` will pass `'first'` to `groupby.aggregate`.

```In : world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In : world = world[['continent', 'geometry']]

In : continents = world.dissolve(by='continent')

In : continents.plot();

Out:
geometry
continent
Africa         MULTIPOLYGON (((32.830 -26.742, 32.580 -27.470...
Antarctica     MULTIPOLYGON (((-163.713 -78.596, -163.713 -78...
Asia           MULTIPOLYGON (((120.295 -10.259, 118.968 -9.55...
Europe         MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ...
North America  MULTIPOLYGON (((-61.680 10.760, -61.105 10.890...
``` If we are interested in aggregate populations, however, we can pass different functions to the `dissolve` method to aggregate populations using the `aggfunc =` argument:

```In : world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

In : world = world[['continent', 'geometry', 'pop_est']]

In : continents = world.dissolve(by='continent', aggfunc='sum')

In : continents.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd');

Out:
geometry     pop_est
continent
Africa         MULTIPOLYGON (((32.830 -26.742, 32.580 -27.470...  1219176238
Antarctica     MULTIPOLYGON (((-163.713 -78.596, -163.713 -78...        4050
Asia           MULTIPOLYGON (((120.295 -10.259, 118.968 -9.55...  4389144868
Europe         MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ...   746398461
North America  MULTIPOLYGON (((-61.680 10.760, -61.105 10.890...   573042112
``` ## Dissolve Arguments¶

The `aggfunc =` argument defaults to ‘first’ which means that the first row of attributes values found in the dissolve routine will be assigned to the resultant dissolved geodataframe. However it also accepts other summary statistic options as allowed by `pandas.groupby()` including:

• ‘first’

• ‘last’

• ‘min’

• ‘max’

• ‘sum’

• ‘mean’

• ‘median’