dtoolkit.geoaccessor.geodataframe.xy#
- dtoolkit.geoaccessor.geodataframe.xy(df: GeoDataFrame, /, reverse: bool = False, frame: bool = True, drop: bool = True, name: Hashable | tuple[Hashable, Hashable] = ('x', 'y')) Series | DataFrame | GeoDataFrame [source]#
Return the x and y location of Point geometries in a GeoDataFrame.
- Parameters:
- reversebool, default False
If True, return (y, x) instead.
- framebool, default True
If True, return a DataFrame.
- dropbool, default True
If True, only return the new generated coordinates.
- nameHashable or a tuple of Hashable, default (‘x’, ‘y’)
If
frame=True
, the column names of the returned DataFrame, else the name of the returned Series.
- Returns:
- Series, DataFrame or GeoDataFrame
If
drop=False
, return a GeoDataFrame with the new generated coordinates.If
drop=True
andframe=True
, return a DataFrame with x and y two columns.If
drop=True
andframe=False
, return a DataFrame with tuple of coordinate.
Examples
>>> import dtoolkit.geoaccessor >>> import geopandas as gpd >>> from shapely import Point >>> df = gpd.GeoDataFrame({ ... "label": ["a", "b", "c"], ... "geometry": [Point(0, 1), Point(0, 2), Point(0, 3)], ... }) >>> df label geometry 0 a POINT (0.00000 1.00000) 1 b POINT (0.00000 2.00000) 2 c POINT (0.00000 3.00000)
Get the x and y coordinates of each point as a tuple.
>>> df.xy(frame=False, name=None) 0 (0.0, 1.0) 1 (0.0, 2.0) 2 (0.0, 3.0) dtype: object
Set
reverse=True
to return (y, x).>>> df.xy(reverse=True, frame=False, name=None) 0 (1.0, 0.0) 1 (2.0, 0.0) 2 (3.0, 0.0) dtype: object
Set
frame=True
to return a DataFrame with x and y columns.>>> df.xy() x y 0 0.0 1.0 1 0.0 2.0 2 0.0 3.0
Keep other columns.
>>> df.xy(drop=False) x y geometry label 0 0.0 1.0 POINT (0.00000 1.00000) a 1 0.0 2.0 POINT (0.00000 2.00000) b 2 0.0 3.0 POINT (0.00000 3.00000) c