【发布时间】:2021-12-20 05:47:44
【问题描述】:
我正在尝试获取数据框的“城市”列的坐标。该城市列的 dtype 为 ('O')。它是一个城市名称列表。我已确保城市名称中没有数字或数字。这是我运行来获取坐标的函数:
def get_coordinates(city_list):
"""Takes a list of cities and returns a dictionary of the cities and their corresponding coordinates."""
geolocator = Nominatim(user_agent='location script')
dicto = {}
for city in city_list:
try:
location = geolocator.geocode(city)
except:
raise Exception("There was a problem with the getCoordinates function")
coordinate_values = (location.longitude, location.latitude) #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
return dicto #finally retruns the dict
然后:
#getting coordinates for each city in the list
city_coords_dict = get_coordinates(city_list)
city_coords_dict
最终输出:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-63-dedf916e4307> in <module>
1 #getting coordinates for each city in the list
----> 2 city_coords_dict = get_coordinates(city_list)
3 city_coords_dict
<ipython-input-62-c391f86b76db> in get_coordinates(city_list)
13 except:
14 raise Exception("There was a problem with the getCoordinates function")
---> 15 coordinate_values = (location.longitude, location.latitude) #in geopandas, the x value corresponds to the longitude while the y value, the latitude(Just in case you were wondering why it was *location.longitude, location.latitude* and not the other way round )
16 dicto[city] = coordinate_values #adding the coordinate pair to the dictionary at the end of every loop
17 return dicto #finally retruns the dict
AttributeError: 'NoneType' object has no attribute 'longitude'
任何有建议的人将不胜感激。提前致谢。
【问题讨论】:
-
您的错误意味着
geolocator.geocode(city)正在为您的一个城市返回None。
标签: python pandas jupyter-notebook coordinates geopy