Posts

Showing posts with the label Geodataframe

Converting Pandas DataFrame To GeoDataFrame

Answer : Convert the DataFrame's content (e.g. Lat and Lon columns) into appropriate Shapely geometries first and then use them together with the original DataFrame to create a GeoDataFrame. from geopandas import GeoDataFrame from shapely.geometry import Point geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)] df = df.drop(['Lon', 'Lat'], axis=1) gdf = GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Result: Date/Time ID geometry 0 4/1/2014 0:11:00 140 POINT (-73.95489999999999 40.769) 1 4/1/2014 0:17:00 NaN POINT (-74.03449999999999 40.7267) Since the geometries often come in the WKT format, I thought I'd include an example for that case as well: import geopandas as gpd import shapely.wkt geometry = df['wktcolumn'].map(shapely.wkt.loads) df = df.drop('wktcolumn', axis=1) gdf = gpd.GeoDataFrame(df, crs="EPSG:4326", geometry=geometry) Update 201912: The official documentation at h...