我正在尝试根据数据帧中的经度和纬度定义位置:
lat long
40.712784 -74.005941
55.755826 37.617300
41.902783 12.496366
我使用的是geopy库,我想将检索位置存储在现有数据框的新第三栏中。像:
lat long location
40.712784 -74.005941 New-York
55.755826 37.617300 Moscow
41.902783 12.496366 Rome
我执行的代码是:
def take_location():
geolocator = Nominatim()
df['location']=''
for row, index in df.iterrows():
location= geolocator.reverse("%f, %f" % (row['lat'], row['long']))
row['location']=location.address
return df
问题发生在这条线上:----> 6 location= geolocator.reverse("%f, %f" % (row['lat'], row['long']))
接下来,IndexError: invalid index to scalar variable.
我想参数可能有问题,并从行单元格中提取值
DataFrame.iterrows()
按顺序给出值(index, row)
--但是您假设它按顺序出现(row,index)
,这是错误的,因此您遇到了问题。您应该使用-
for index, row in df.iterrows():
location= geolocator.reverse("%f, %f" % (row['lat'], row['long']))
row['location']=location.address
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句