pandas python中的地理编码使用API密钥从地址获取纬度和经度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47283197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geocoding in python get Latitude and Longitude from addresses using API key
提问by Harry
I currently have a data frame that has address details of certain place. I want to use Google geocode API key to find the coordinates - Latitude and Longitude in order to plot a map. Does anybody know how to do this? I have tried the below code but it is returning 'Error, skipping address...' on all the lines of addresses.
我目前有一个数据框,其中包含某个地方的地址详细信息。我想使用 Google 地理编码 API 密钥来查找坐标 - 纬度和经度以绘制地图。有人知道怎么做这个吗?我已经尝试了下面的代码,但它在所有地址行上都返回“错误,正在跳过地址...”。
I would greatly appreciate any help!
我将不胜感激任何帮助!
import pandas as pd
import os
from geopy import geocoders
from geopy.geocoders import GoogleV3
API_KEY = os.getenv("API1234")
g = GoogleV3(api_key=API_KEY)
loc_coordinates = []
loc_address = []
for address in df.Address:
try:
inputAddress = Address
location = g.geocode(inputAddress, timeout=15)
loc_coordinates.append((location.latitude, location.longitude))
loc_Address.append(inputAddress)
except:
print('Error, skipping address...')
df_geocodes = pd.DataFrame({'coordinate':loc_coordinates,'address':loc_address})
回答by Robert D. Mogos
You had some typos: Address
instead of address
, loc_Address
instead of loc_address
.
您有一些拼写错误:Address
而不是address
,loc_Address
而不是loc_address
。
But what is df.Address
?
但什么是df.Address
?
Try this:
尝试这个:
import pandas as pd
import os
from geopy import geocoders
from geopy.geocoders import GoogleV3
API_KEY = os.getenv("API1234")
g = GoogleV3(api_key=API_KEY)
loc_coordinates = []
loc_address = []
for address in df.Address:
try:
inputAddress = address
location = g.geocode(inputAddress, timeout=15)
loc_coordinates.append((location.latitude, location.longitude))
loc_address.append(inputAddress)
except Exception as e:
print('Error, skipping address...', e)
df_geocodes = pd.DataFrame({'coordinate':loc_coordinates,'address':loc_address})