I'm trying to perform an operation on a whole column but I'm getting a type error, I want to make a column containing a Shapely Point:
crime_df = crime_df[crime_df['Latitude'].notna()]
crime_df = crime_df[crime_df['Longitude'].notna()]
crime_df['Longitude'] = crime_df['Longitude'].astype(float)
crime_df['Latitude'] = crime_df['Latitude'].astype(float)
print (crime_df['Longitude'])
print (crime_df['Latitude'])
crime_df['point'] = Point(crime_df['Longitude'], crime_df['Latitude'])
Output:
18626 -87.647379
Name: Longitude, Length: 222, dtype: float64
18626 41.781100
Name: Latitude, Length: 222, dtype: float64
TypeError: cannot convert the series to <class 'float'>
I think you need working with each point separately, so need DataFrame.apply
with lambda function:
crime_df['point'] = crime_df.apply(lambda x: Point(x['Longitude'], x['Latitude'], axis=1)
Or thanks @N. Wouda:
crime_df["point"] = crime_df[["Longitude", "Latitude"]].apply(Point, axis=1)
Or list comprehension alternative is:
crime_df['point'] = [Point(lon, lat)
for lon, lat in crime_df[['Longitude','Latitude']].values]
EDIT: I think for vectorized way is possible use geopandas.points_from_xy
like:
gdf = geopandas.GeoDataFrame(df,geometry=geopandas.points_from_xy(df.Longitude,df.Latitude))