Shapely point geometry in geopandas df to lat/lon columns
If you have the latest version of geopandas (0.3.0 as of writing), and the if df
is a GeoDataFrame, you can use the x
and y
attributes on the geometry column:
df['lon'] = df.point_object.xdf['lat'] = df.point_object.y
In general, if you have a column of shapely objects, you can also use apply
to do what you can do on individual coordinates for the full column:
df['lon'] = df.point_object.apply(lambda p: p.x)df['lat'] = df.point_object.apply(lambda p: p.y)
Without having to iterate over the Dataframe, you can do the following:
df['lon'] = df['geometry'].xdf['lat'] = df['geometry'].y
The solution to extract the center point (latitude and longitude) from the polygon and multi-polygon.
import geopandas as gpddf = gpd.read_file(path + 'df.geojson')#Find the center pointdf['Center_point'] = df['geometry'].centroid#Extract lat and lon from the centerpointdf["long"] = df.Center_point.map(lambda p: p.x)df["lat"] = df.Center_point.map(lambda p: p.y)