geopandas.GeoDataFrame.explode#

GeoDataFrame.explode(column=None, ignore_index=False, index_parts=False, **kwargs)[source]#

Explode multi-part geometries into multiple component geometries.

Each row containing a multi-part geometry will be split into multiple rows with each component geometry, thereby increasing the vertical size of the GeoDataFrame.

GeometryCollections are split into their direct components. If a GeometryCollection contains a multi-part geometry, that component is returned as a multi-part geometry. To further split multi-part geometries within a GeometryCollection, call explode a second time.

Parameters:
columnstring or list of strings, default None

Column(s) to explode. In the case of a geometry column, multi-part geometries are converted to their component geometries. If None, the active geometry column is used. A list of multiple (non-geometry) columns may be passed to use the pandas multi-column explode.

ignore_indexbool, default False

If True, the resulting index will be labelled 0, 1, …, n - 1, ignoring index_parts.

index_partsboolean, default False

If True, the resulting index will be a multi-index (original index with an additional level indicating the multiple geometries: a new zero-based index for each component geometry per input geometry).

Returns:
GeoDataFrame

Exploded geodataframe with each component geometry as a separate entry in the geodataframe.

See also

GeoDataFrame.dissolve

dissolve geometries into a single observation.

Examples

>>> from shapely.geometry import MultiPoint
>>> d = {
...     "col1": ["name1", "name2"],
...     "geometry": [
...         MultiPoint([(1, 2), (3, 4)]),
...         MultiPoint([(2, 1), (0, 0)]),
...     ],
... }
>>> gdf = geopandas.GeoDataFrame(d, crs=4326)
>>> gdf
    col1                   geometry
0  name1  MULTIPOINT ((1 2), (3 4))
1  name2  MULTIPOINT ((2 1), (0 0))
>>> exploded = gdf.explode(index_parts=True)
>>> exploded
      col1     geometry
0 0  name1  POINT (1 2)
  1  name1  POINT (3 4)
1 0  name2  POINT (2 1)
  1  name2  POINT (0 0)
>>> exploded = gdf.explode(index_parts=False)
>>> exploded
    col1     geometry
0  name1  POINT (1 2)
0  name1  POINT (3 4)
1  name2  POINT (2 1)
1  name2  POINT (0 0)
>>> exploded = gdf.explode(ignore_index=True)
>>> exploded
    col1     geometry
0  name1  POINT (1 2)
1  name1  POINT (3 4)
2  name2  POINT (2 1)
3  name2  POINT (0 0)

A single explode call splits a GeometryCollection into its direct components, which may themselves be multi-part geometries:

>>> from shapely.geometry import GeometryCollection
>>> gdf = geopandas.GeoDataFrame(
...     {
...         "geometry": [
...             GeometryCollection(
...                 [
...                     MultiPoint([(1, 2), (3, 4)]),
...                     MultiPoint([(2, 1), (0, 0)]),
...                 ]
...             )
...         ]
...     },
...     crs=4326,
... )
>>> gdf.explode(index_parts=False)
                    geometry
0  MULTIPOINT ((1 2), (3 4))
0  MULTIPOINT ((2 1), (0 0))