迭代数据帧行时出错

时间:2015-10-21 16:18:30

标签: python for-loop pandas dataframe

我正在尝试定义纬度和经度的位置,我在数据帧中有:

    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.   我想参数可能有问题,并从行单元格中提取值

1 个答案:

答案 0 :(得分:1)

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