【发布时间】:2021-11-20 22:33:44
【问题描述】:
我的 DataFrame 'df' 有一个带有城市名称的列 df['Cidade'],我想使用 Geopy 找出每个城市的纬度和经度。以下代码适用于单个城市输入:
address='Pacoti'
geolocator = Nominatim(user_agent="Your_Name")
location = geolocator.geocode(address)
print(location.address)
print((location.latitude, location.longitude))
输出:
Pacoti, Região Geográfica Imediata de Redenção-Acarape, Região Geográfica Intermediária de Fortaleza, Ceará, Região Nordeste, 62770-000,巴西(-4.2253561,-38.9208352)
所以我尝试创建一个函数:
def lat_long(x):
address= x
geolocator = Nominatim(user_agent="Your_Name")
location = geolocator.geocode(address)
return location.latitude, location.longitude
然后我使用'map'函数:
df['Cidade'].map(lat_long)
但我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'latitude'
我做错了什么?
【问题讨论】:
标签: python pandas map-function geopy nominatim