我有这个数组:
引擎盖
array([['Altos de Arroyo Hondo ,Santo Domingo'],
['Arroyo Manzano ,Santo Domingo'],
['Atala ,Santo Domingo'],
['Bella Vista ,Santo Domingo'],
['Buenos Aires-Independencia ,Santo Domingo'],
['Cacique ,Santo Domingo'],
['Centro de los Héroes ,Santo Domingo'],
['Centro Olímpico Duarte ,Santo Domingo'],
['Cerros de Arroyo Hondo ,Santo Domingo'],
['Ciudad Colonial ,Santo Domingo'],
['Ciudad Nueva ,Santo Domingo'],
['Veinticuatro de Abril ,Santo Domingo'],
['Viejo Arroyo Hondo ,Santo Domingo'],
['Villa Consuelo ,Santo Domingo'],
['Villa Francisca ,Santo Domingo'],
['Villa Juana ,Santo Domingo'],
['Villas Agricolas ,Santo Domingo'],
['Distrito Nacional ,Santo Domingo']], dtype=object)
然后我创建了一个空的数据框:
hoods_located = pd.DataFrame(columns =['Neighborhood', 'Latitude', 'Longitude'])
我要完成的是从for循环的结果中在该数据框中插入值:
for i in hoods:
geolocator
location = geolocator.geocode(i)
latitude = location.latitude
longitude = location.longitude
hoods_located.append({'Neighborhood': i, 'Latitude': latitude, 'Longitude': longitude}, ignore_index=True)
使用Geopy,for循环为hoods数组的每个值查找Lat和Lon,但无法弄清楚如何将这些值附加到新的数据框hoods_located。
谢谢。
答案 0 :(得分:0)
我建议您使用defaultdict将您的数据放入字典,然后直接读入数据框;它还消除了创建空白数据框来填充的需求:
from collections import defaultdict
d = defaultdict(list)
#iterate through hood list and create dictionary
for Neighbourhood in hoods:
location = geolocator.geocode(Neighbourhood)
d['latitude'].append(location.latitude)
d['longitude'].append(location.longitude)
d['Neighbourhood'].append(Neighbourhood)
#read into dataframe
pd.DataFrame(d)
仅在此处使用您的代码工作,我可能已经错过了一个步骤或更长时间...让我知道它是否有效