如何将两个坐标列转换为一列 Shapely 点 [英] How to convert two coordinate columns to a column of Shapely points

查看:104
本文介绍了如何将两个坐标列转换为一列 Shapely 点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对整个列执行操作,但出现类型错误,我想创建一个包含 Shapely Point:

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'])

输出:

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 函数:

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)

或者感谢@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 喜欢:

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))

这篇关于如何将两个坐标列转换为一列 Shapely 点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆