Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x &引用;[Index([';纬度';,';经度';],dtype=';对象';)均不在[Index]”中;_Python 3.x_Pandas - Fatal编程技术网

Python 3.x &引用;[Index([';纬度';,';经度';],dtype=';对象';)均不在[Index]”中;

Python 3.x &引用;[Index([';纬度';,';经度';],dtype=';对象';)均不在[Index]”中;,python-3.x,pandas,Python 3.x,Pandas,我有一个带有geolocations的pandas数据框,我正在尝试创建一个列,并向它传递一个函数,该函数将获得每个位置的walkscores 这是我的数据帧: 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.29

我有一个带有geolocations的pandas数据框,我正在尝试创建一个列,并向它传递一个函数,该函数将获得每个位置的walkscores

这是我的数据帧:

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]"

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

删除
loc
,因为需要查看LCT列,而不是索引值:

df_test['walkscore'] = df_test.\
    apply(lambda x: walkscore(x['latitude'], x['longitude'], apikey), axis='columns')
使用示例函数验证:

apikey = 'aaa'
def walkscore(x, y, apikey):
    return tuple((x, y))

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

print (df_test)
     latitude   longitude                                  walkscore
0   50.673170 -120.322639                    (50.67317, -120.322639)
1   50.669597 -120.341833  (50.669596999999996, -120.34183300000001)
2   50.650727 -120.150661           (50.650727, -120.15066100000001)
3   50.687545 -120.297688                   (50.687545, -120.297688)
4   50.772361 -122.811211           (50.772361, -122.81121100000001)
5   50.882304 -119.865000                      (50.882304, -119.865)
6   50.643431 -120.362385                   (50.643431, -120.362385)
7   50.707459 -120.376297                   (50.707459, -120.376297)
8   50.708614 -120.409419          (50.708614000000004, -120.409419)
9   50.697850 -120.389101                    (50.69785, -120.389101)
10  50.659250 -119.998597                    (50.65925, -119.998597)

感谢您的回复@jezrael。当我运行该函数时,它会在响应行中给我一个walkscore值列表,但是,当我查找walkscore列时,它都是Nonevalues@user4718221-如果使用我的示例函数
def walkscore(x,y,apikey):返回元组((x,y))
it working?它是。我检查了API,它工作正常,只是不能将正确的结果放入数据帧中somehow@user4718221-是的,我只想测试,如果使用我的自定义
walkscore
函数获得与我的答案相同的输出。没关系,我已经修复了它。我使用打印(walkscore)而不是返回,这样它就不会向数据帧注册任何内容。非常感谢你的帮助!