【问题标题】:Applying lambda function on two columns in a Dataframe to get Geolocation在数据框中的两列上应用 lambda 函数以获取地理位置
【发布时间】:2017-04-15 17:58:41
【问题描述】:

数据集:

我正在尝试使用坐标 Pickup_longitude 和 Pickup and latitude 来获取上车地点。如果我单独调用该函数,它可以工作。如果我将 pandas 与 lambda 一起调用,则它不起作用。

import requests
def fxy(x, y):
    url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+x+","+y+"&sensor=true"
    print(url)
    result=requests.get(url).json()
    str=result['results'][0]['address_components'][3]['long_name']
    print(str)
    return str

#str1=fxy('40.69238281','-73.94309998')
#str1

locations=pd.read_csv("Data/locations.csv",sep=",")
locations['pickup_location'] = locations.apply(lambda x: fxy(str(x['Pickup_longitude']), str(x['Pickup_latitude'])))

错误:

---> 5 url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+x,y+"&sensor=true" 6 打印(网址) 7 结果=requests.get("http://maps.googleapis.com/maps/api/geocode/json?latlng="+x+","+y+"&sensor=true").json()

TypeError: ("ufunc 'add' 不包含带有签名的循环 匹配类型 dtype('

【问题讨论】:

标签: python-3.x pandas numpy dataframe lambda


【解决方案1】:

将数据框列转换为字符串,它可以工作。

locations['pickup_location'] = locations.apply(lambda x: fxy(str(x['Pickup_latitude']),str(x['Pickup_longitude'])),axis=1)

【讨论】:

    猜你喜欢
    • 2017-06-20
    • 2017-05-15
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多