Javier Monsalve
Javier Monsalve

Reputation: 326

Create column in geopandas dataframe using assign method

I have the following piece of code which works perfectly:

gdf = gpd.GeoDataFrame(df,crs={'init': 'epsg:4326'})
gdf["geometry"] = gdf.apply(lambda x: Point(x.longitud, x.latitud),axis=1)

I would like to create the column 'geometry' using the assign method but I don't know how. I have tried something like the following but it does not work.

gdf = gpd.GeoDataFrame(df,crs={'init': 'epsg:4326'})\
.assign(geometry, apply(lambda x: Point(x.longitud, x.latitud),axis=1))

Upvotes: 0

Views: 1630

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 30050

gdf = gpd.GeoDataFrame(df, crs='epsg:4326').assign(geometry=lambda x: [Point(lon, lat) for lon, lat in zip(x.longitud.tolist(), x.latitud.tolist())])

Upvotes: 2

Related Questions