geopandas.GeoDataFrame.set_geometry

GeoDataFrame.set_geometry(col, drop=False, inplace=False, crs=None)

Set the GeoDataFrame geometry using either an existing column or the specified input. By default yields a new object.

The original geometry column is replaced with the input.

Parameters
colcolumn label or array
dropboolean, default False

Delete column to be used as the new geometry

inplaceboolean, default False

Modify the GeoDataFrame in place (do not create a new object)

crspyproj.CRS, optional

Coordinate system to use. The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string. If passed, overrides both DataFrame and col’s crs. Otherwise, tries to get crs from passed col values or DataFrame.

Returns
GeoDataFrame

See also

GeoDataFrame.rename_geometry

rename an active geometry column

Examples

>>> from shapely.geometry import Point
>>> d = {'col1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf
    col1                 geometry
0  name1  POINT (1.00000 2.00000)
1  name2  POINT (2.00000 1.00000)

Passing an array:

>>> df1 = gdf.set_geometry([Point(0,0), Point(1,1)])
>>> df1
    col1                 geometry
0  name1  POINT (0.00000 0.00000)
1  name2  POINT (1.00000 1.00000)

Using existing column:

>>> gdf["buffered"] = gdf.buffer(2)
>>> df2 = gdf.set_geometry("buffered")
>>> df2.geometry
0    POLYGON ((3.00000 2.00000, 2.99037 1.80397, 2....
1    POLYGON ((4.00000 1.00000, 3.99037 0.80397, 3....
Name: buffered, dtype: geometry