gpt4 book ai didi

python - 如何将两个坐标列转换为一列 Shapely 点

转载 作者:行者123 更新时间:2023-12-04 01:30:15 25 4
gpt4 key购买 nike

我正在尝试对整个列执行操作,但出现类型错误,我想创建一个包含 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'])

输出:
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'>

最佳答案

我认为您需要分别处理每个点,因此需要 DataFrame.apply 使用 lambda 函数:

crime_df['point'] = crime_df.apply(lambda x: Point(x['Longitude'], x['Latitude'], axis=1)

或者感谢@N。沃达:
crime_df["point"] = crime_df[["Longitude", "Latitude"]].apply(Point, axis=1)

或者列表理解替代是:
crime_df['point'] = [Point(lon, lat) 
for lon, lat in crime_df[['Longitude','Latitude']].values]

编辑:我认为可以使用矢量化方式 geopandas.points_from_xy 喜欢:
gdf = geopandas.GeoDataFrame(df,geometry=geopandas.points_from_xy(df.Longitude,df.Latitude))

关于python - 如何将两个坐标列转换为一列 Shapely 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61170839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com