Tom*_*eck 6 python point pandas shapely
我正在尝试对整个列执行操作,但出现类型错误,我想创建一个包含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'])
Run Code Online (Sandbox Code Playgroud)
输出:
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'>
Run Code Online (Sandbox Code Playgroud)
我认为你需要分别处理每个点,所以需要DataFrame.apply使用 lambda 函数:
crime_df['point'] = crime_df.apply(lambda x: Point(x['Longitude'], x['Latitude'], axis=1)
Run Code Online (Sandbox Code Playgroud)
或者谢谢@N。沃达:
crime_df["point"] = crime_df[["Longitude", "Latitude"]].apply(Point, axis=1)
Run Code Online (Sandbox Code Playgroud)
或者列表理解替代方案是:
crime_df['point'] = [Point(lon, lat)
for lon, lat in crime_df[['Longitude','Latitude']].values]
Run Code Online (Sandbox Code Playgroud)
编辑:我认为对于矢量化方式可以使用geopandas.points_from_xy如下:
gdf = geopandas.GeoDataFrame(df,geometry=geopandas.points_from_xy(df.Longitude,df.Latitude))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5129 次 |
| 最近记录: |