在下面的代码块中,我有一个数据帧geo
,我想对它进行迭代以获得geo
中每个英国邮政编码的东距、北距、经度和纬度。我编写了一个函数来调用API,另一个函数返回四个变量
我已经用邮政编码测试了get_data
调用,以证明它是有效的(这是任何人都可以使用的公共API):
import requests
import pandas as pd
geo = spark.table('property_address').toPandas()
def call_api(url: str) -> dict:
postcode_response =requests.get(url)
return postcode_response.json()
def get_data(postcode):
url = f"http://api.getthedata.com/postcode/{postcode}"
req = r.get(url)
results = req.json()['data']
easting = results['easting']
northing = results['northing']
latitude = results['latitude']
longitude = results ['longitude']
return easting ,northing,latitude, longitude
get_data('SW1A 1AA')
返回:
Out[108]: (529090, 179645, '51.501009', '-0.141588')
我想做的是为geo
中的每一行运行它,并将其作为数据集返回。我的研究让我找到了apply
,我的尝试基于this guide
我试图在geo
中传递一个名为property_postcode
的列,并迭代每一行以返回值,以下是我的尝试:
def get_columns(row):
column_name = 'property_postcode'
api_param = row[column_name]
easting,northing,latitude,longitude = get_data(api_param)
row['east'] = easting
row['north'] = northing
row['lat'] = latitude
row['long'] = longitude
return row
geo= geo.apply(get_columns, axis=1)
display(geo)
我得到的错误是
`JSONDecodeError: Expecting value: line 1 column 1 (char 0)`
没告诉我多少。正在寻找帮助\指针
而不是试图设置函数中east、north、lat和long列的值,而是从函数返回它们
相关问题 更多 >
编程相关推荐