gpt4 book ai didi

python - 使用python提取纬度和经度

转载 作者:行者123 更新时间:2023-12-01 07:06:41 25 4
gpt4 key购买 nike

我正在尝试从实际地址中提取纬度和经度。

我看过一些视频可以帮助我编写以下脚本,但我有一个错误显示

Lat=Geocode_Result[0]["geometry"]["Location"]["Lat"]
KeyError: 'Location'
  import pandas as pd
import googlemaps code
df=pd.DataFrame({'Address':['9 RUE DU FOSSE, L-7772, luxembourg', '37 RUE DE LA GARE,
L-7535, luxembourg']}) # Creating a dataframe with 2 addresses (its just an example)
Gmaps_key=googlemaps.Client("xxxxxxxxx")

df['Latitude'] = None
df['Longitude'] = None

for i in range(len(df)):
Geocode_Result=Gmaps_key.geocode(df.loc[i,'Address']) # sending a request for each of the addresses

Lat=Geocode_Result[0]["geometry"]["Location"]["Lat"] # Extracting the Latitude information
Long=Geocode_Result[0]["geometry"]["Location"]["Long"] # Extracting the Longitude information
df.loc[i,'Latitude'] = Lat # Pass the information into the DataFrame
df.loc[i,'Longitude'] = Long

print(df)

最佳答案

您需要首先安装 geopy (pip install geopy) 并导入 Nominatim。我对您的代码做了一些修改以获得输出。

import pandas as pd
from geopy.geocoders import Nominatim

df=pd.DataFrame({'Address':['9 RUE DU FOSSE, L-7772, luxembourg', '37 RUE DE LA GARE,L-7535, luxembourg']})
df['Latitude'] = None
df['Longitude'] = None
locator = Nominatim(user_agent="myGeocoder")
for i in range(len(df)):
location = locator.geocode(df.loc[i,'Address'])
df.loc[i,'Latitude'] = location.latitude
df.loc[i,'Longitude'] = location.longitude
print(df)

输出:

                                 Address Latitude Longitude
0 9 RUE DU FOSSE, L-7772, luxembourg 49.789 6.06569
1 37 RUE DE LA GARE,L-7535, luxembourg 49.7684 5.52118

关于python - 使用python提取纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419206/

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