“ [Index(['latitude','longitude'],dtype ='object')]都不在[index]中]

问题描述 投票:0回答:1

我有一个带有地理位置的pandas数据框,我试图创建一列并将其传递给该列,该函数将为每个位置获取走分。

这是我的数据框:

df_test[['latitude', 'longitude']]

    latitude    longitude
0   50.673170   -120.322639
1   50.669597   -120.341833
2   50.650727   -120.150661
3   50.687545   -120.297688
4   50.772361   -122.811211
5   50.882304   -119.865000
6   50.643431   -120.362385
7   50.707459   -120.376297
8   50.708614   -120.409419
9   50.697850   -120.389101
10  50.659250   -119.998597

当我在单个变量上测试该函数时,一切正常:

walkscore(df_test['latitude'][0], df_test['longitude'][0], key)

71

但是当我尝试通过以下方式将此函数传递给整个数据集时,出现错误:

df_test.loc['walkscore'] = df_test.loc[['latitude', 'longitude']].\
    apply(lambda x:
                    walkscore(x['latitude'], x['longitude'], apikey), axis='columns')

KeyError: "None of [Index(['latitude', 'longitude'], dtype='object')] are in the [index]"

我尝试重置索引,但没有帮助。我在这里做错了吗?

python-3.x pandas
1个回答
0
投票

删除loc,因为需要填充列,而不是索引值:

df_test['walkscore'] = df_test[['latitude', 'longitude']].\
    apply(lambda x: walkscore(x['latitude'], x['longitude'], apikey), axis='columns')
© www.soinside.com 2019 - 2024. All rights reserved.