我正在使用Pycharm 2.2 community
,目前正在尝试从地址中获取latitude
和longitude
。将地址数据存储在熊猫数据框中,并尝试获取其地理坐标。但是,它返回了我:
"Traceback (most recent call last):
File "C:/map.py", line 19, in <module>
geocode_result = gmaps_key.geocode(data1.iat[i, 0])" and "return self.obj._get_value(*key, takeable=self._takeable)" after execution.
将鼠标悬停在地理编码上时,工具提示返回"unresolved attribute reference 'geocode'
类的我'Client'
...”
下面是我的代码,非常感谢您的帮助!
import pandas as pd
import googlemaps
data = pd.read_csv("listing.csv", usecols=['building_no', 'street_name'])
# df = pd.DataFrame({'Year': ['2014', '2015'], 'quarter': ['q1', 'q2']})
data['address'] = data[['building_no', 'street_name']].apply(lambda x: ' '.join(x), axis=1)
data1 = data['address']
# print data1
# Set google map API key
gmaps_key = googlemaps.Client(key="AIzaSyDjB0HJolcomNZCWrtq9gef70V4F2xtB_s")
# create Geocode result object
# get LON and LAT
data1["LAT"] = None
data1["LON"] = None
for i in range(0, len(data1), 1):
geocode_result = gmaps_key.geocode(data1.iat[i, 0])
try:
lat = geocode_result[0]["geometry"]["location"]["lat"]
lon = geocode_result[0]["geometry"]["location"]["lng"]
data1.iat[i, data1.columns.get_loc("LAT")] = lat
data1.iat[i, data1.columns.get_loc("LON")] = lon
except:
lat = None
lon = None
print data1