我有一个包含以下列的数据框:
link sqft rent bedroom address1 address2 address3 bathroom
我有一个函数,它接受3个地址作为输入,然后以dict
的形式返回纬度和经度
当我应用这样的函数时
df['lat'] = return_coordinates(df.address1,df.address2,df.addresse3).get('Latitude')
。。。lat
列包含相同的值
下面是函数:
import herepy
geocoderApi = herepy.GeocoderApi('ap_key')
def return_coordinates(address1,address2,address3):
response = geocoderApi.free_form('{},{},{}'.format(address1,address2,address3))
geocode = response.as_dict()
geocode = geocode.get('Response')
geocode = geocode.get('View')
value = (geocode[0].get('Result')[0].get('Location').get('DisplayPosition'))
return value
你的代码应该可以工作。以下是您正在寻找的示例,以防万一:
输出:
下面是一个使用
DataFrame.apply()
函数的解决方案Documentation can be found here.由于缺少数据和对API的访问,我假装您是数据集,尽管效果很差。但这演示了如何使用
apply()
函数从函数的返回值填充lat
列基本(黑客)设置:
使用
apply
函数:这将广播
return_coordinates
函数返回到数据帧的lat
列原始数据集:
使用新的
lat
列:从结果中可以看到,
apply
函数为每一行返回address1
字段的第一个值相关问题 更多 >
编程相关推荐