如何使用Google的Geocode API和批量地址

时间:2018-05-11 21:07:17

标签: python pandas google-maps apply google-geocoder

我尝试从地址列表开始,调用Google的Geocode API,并为每个地址返回相应的经度和纬度。到目前为止,这就是我所拥有的:

import requests
import pandas as pd`
#List of addresses and make dataframe
address1=['100 20th St. Seattle WA','300 Main St. Washington DC','100 Main 
St Richmond VA 23220']`

address1=pd.DataFrame(address1,columns=['Address'])`
#Set up API key
api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXX"
api_response = 
requests.get('https://maps.googleapis.com/maps/api/geocode/json?address= 
{0}&key={1}'.format(address1, api_key))
api_response_dict = api_response.json()`

#create function for calling API
def get_long_lat(address):
  if api_response_dict['status'] == 'OK':
   latitude = api_response_dict['results'][0]['geometry']['location']['lat']
   longitude = api_response_dict['results'][0]['geometry']['location'] 
   ['lng']
   return latitude, longitude `

#attempts a passing the dataframe through the function 

address1['attempt1']=address1.apply(get_long_lat,axis=1)
address1['attempt2'] = address1.apply(lambda x: get_long_lat(x),axis=1)
address1['attempt3']= address1.groupby('Address').apply(get_long_lat)
address1['attempt4']=address1['Address'].apply(get_long_lat)`

output image

我正在尝试逐行调用该函数,但问题是它返回第一个地址的long和lat并将其复制到其他地址。这是使用pandas数据帧或.apply函数还是API函数的问题?

0 个答案:

没有答案