我正在制作热图以进行绘制使用Google谷歌地理编码API的城市。我遵循此链接enter link description here,因为我具有相同类型的数据。我在Google Developer页面中激活了API,并创建了一个密钥来使用它。我使用笔记本jupyter分别运行代码。
首先阻止它运行良好:
import pandas as pd
import numpy as np
import googlemaps
import gmaps
API_KEY = 'Geocoding API'
gm = googlemaps.Client(key='key api ......')
第二个块成功运行:
data = pd.read_csv('dataset.csv', encoding = "ISO-8859-1", low_memory=False)
data = data.fillna('') # fill empty entries with ''
print(list(data)) # print Variable Name
data.head()
第三次阻止它也运行良好
def Geocode(query):
# do geocoding
try:
geocode_result = gm.geocode(query)[0]
latitude = geocode_result['geometry']['location']['lat']
longitude = geocode_result['geometry']['location']['long']
return latitude,longitude
except IndexError:
return 0
def GeocodeStreetLocationCity(data):
lat=[] # initialize latitude list
lng=[] # initialize longitude list
start = data.index[0] # start from the first data
end = data.index[maxRow-1] # end at maximum number of row
for i in range(start,end+1,1): # iterate all rows in the data
isSuccess=True # initial Boolean flag
query = data.lat[i] + ' ' + data.long[i] # try set up our query street-location-city
result=Geocode(query)
if result==0: # if not successful,
query = data.lat[i] + ' ' + data.long[i] # try set up another query location-city
result=Geocode(query)
if result==0: # if still not successful,
query = data.lat[i] + ' ' + data.long[i] # try set up another query street-city
result=Geocode(query)
if Geocode(query)==0: # if still not successful,
isSuccess=False # mark as unsuccessful
print(i, 'is failed')
else:
print(i, result)
else:
print(i, result)
else:
print(i, result)
if isSuccess==True: # if geocoding is successful,
# store the results
lat.append(result[0]) # latitude
long.append(result[1]) # longitude
return lat,long
运行它时,我在最后一个块中遇到了错误:
# call the geocoding function
[lat,long]=GeocodeStreetLocationCity(data)
# we put the list of latitude,longitude into pandas data frame
df = pd.DataFrame(
{'latitude': lat,
'longitude': long
})
错误是:
运行代码时,它在最后一个块中返回错误:
TypeError Traceback (most recent call last)
<ipython-input-67-d174cb0d8b65> in <module>
1 # call the geocoding function
----> 2 [lat,long]=GeocodeStreetLocationCity(data)
---> 18 query = data.lat[i] + ' ' + data.long[i] # try set up our query street-location-city
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
我认为我有一个字符串数组,而不是浮点数。这就是dtype('
有人可以建议我一个解决方案吗?
谢谢