对数据帧中的每一行应用邮政编码API调用

2024-05-18 20:54:34 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码块中,我有一个数据帧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)`

没告诉我多少。正在寻找帮助\指针


Tags: apiurldatagetreturndefpropertyresults
1条回答
网友
1楼 · 发布于 2024-05-18 20:54:34

而不是试图设置函数中east、north、lat和long列的值,而是从函数返回它们

from numpy import result_type
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 = requests.get(url)

    if req.json()["status"] == "match":
        results = req.json()["data"]
        easting = results.get("easting")
        northing = results.get("northing")
        latitude = results.get("latitude")
        longitude = results.get("longitude")
    else:
        easting = None
        northing = None
        latitude = None
        longitude = None

    return easting, northing, latitude, longitude


def get_columns(code):
    api_param = code
    return get_data(api_param)


df = pd.DataFrame(
    {
        "property_postcode": [
            "BE21 6NZ",
            "SW1A 1AA",
            "W1A 1AA",
            "DE21",
            "B31",
            "ST16 2NY",
            "S65 1EN",
        ]
    }
)

df[["east", "north", "lat", "long"]] = df.apply(
    lambda row: get_columns(row["property_postcode"]), axis=1, result_type="expand"
)

print(df)

^{tb1}$

相关问题 更多 >

    热门问题