Skip to main content
 首页 » 编程设计

python之反向geopy地理编码 Pandas

2025年02月15日7Free-Thinker

我在 Jupyter Notebook 中有以下数据框,其中包含一个 GPS 坐标列表,其中包含 from geopy.geocoders import Nominatim 和 import pandas as pd。

    stop_id     Lat         Long 
0   2        53.352280  -6.263668 
1   3        53.352345  -6.263758 
2   4        53.352604  -6.264143 
3   6        53.352783  -6.264417 
4   7        53.352867  -6.264543 
5   8        53.353287  -6.265152 

我一直在尝试添加一个新列,其中填充了 GPS 坐标的相应地址。

为此我尝试过

df['address'] = geolocator.reverse((df['Lat'], df['Long'])) 

但得到以下错误信息:

ValueError: Must be a coordinate pair or Point.

然后我创建了另一列 [LatLong]

df['LatLong'] = df[df.columns[1:]].apply( 
    lambda x: ', '.join(x.dropna().astype(float).astype(str)),axis=1) 
 
    stop_id     Lat         Long         LatLong 
0   2       53.352280   -6.263668    53.35228, -6.263668 
1   3       53.352345   -6.263758    53.352345, -6.263758 
2   4       53.352604   -6.264143    53.352604, -6.264143 
3   6       53.352783   -6.264417    53.352783, -6.264417 
4   7       53.352867   -6.264543    53.352867, -6.264543 
5   8       53.353287   -6.265152    53.353287, -6.265152 

然后我运行了下面的代码:

df['address'] = geolocator.reverse(df['LatLong']) 

但是,我只收到完全相同的错误消息。

我上面使用的代码改编自本网站上对类似问题和 GeoPy 文档的其他答案,因此我认为我的代码不够准确,无法以正确的方式为 geopy 提取 GPS 坐标。

谁能指出我的错误吗?

请您参考如下方法:

问题

您的错误信息是:

ValueError: Must be a coordinate pair or Point

在两者中:

df['address'] = geolocator.reverse((df['Lat'], df['Long'])) 

和

df['address'] = geolocator.reverse(df['LatLong']) 

您正在将 pandas 结构发送到一个不理解它们的方法中。

解决方案

我无法对此进行测试,但解决方案可能类似于:

df['address'] = df.apply( 
    lambda row: geolocator.reverse((row['Lat'], row['Long'])), axis=1)