反向地理编码Python-Datafram

2024-04-20 14:01:33 发布

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

我目前有一个数据框,其中列为“纬度”(float)和“经度”(float),我想创建一个列“ZipCode”,其中包含所属的邮政编码。你知道吗

到目前为止,我有以下代码:

location = geolocator.reverse("41.750722, -73.997276")
geo_string = location.address.split(",")
geo_string[-2]

但是,我不知道如何为数据帧中的每一行运行它。 谢谢你的帮助!你知道吗


Tags: 数据代码stringaddresslocationfloatreversegeo
2条回答

不完全确定您的数据帧是什么样子,但我相信您应该能够使用.iterrows()

for index, row in df.iterrows():
    location = geolocator.reverse("{}, {}".format(row['Latitude'], row['Longitude']))
    geo_string = location.address.split(",")
    geo_string[-2]```

我对地球物理学不熟悉。但假设定义了地理定位器,您可以这样做一个单行程序。你知道吗

df = pd.DataFrame([[41.750722, -73.997276]], columns=["Latitude", "Longitude"])
df["ZipCode"] = df.apply(lambda x:geolocator.reverse(str(x["Latitude"])+", "+str(x["Longitude"])).address.split(",")[-2], axis=1)

相关问题 更多 >