【问题标题】:Reverse Geocoding Python Pandas Dataframe反向地理编码 Python Pandas 数据框
【发布时间】:2019-11-29 17:05:10
【问题描述】:
我目前有一个 DataFrame,其中包含“Latitude”(浮点数)和“Longitude”(float)列,我想创建一个包含所属邮政编码的“ZipCode”列。
到目前为止,我有以下代码:
location = geolocator.reverse("41.750722, -73.997276")
geo_string = location.address.split(",")
geo_string[-2]
但是,我无法弄清楚如何为 DataFrame 中的每一行运行它。
感谢您的帮助!
【问题讨论】:
标签:
python
pandas
reverse-geocoding
zipcode
【解决方案1】:
不完全确定您的数据框是什么样的,但我相信您应该能够使用.iterrows():
for index, row in df.iterrows():
location = geolocator.reverse("{}, {}".format(row['Latitude'], row['Longitude']))
geo_string = location.address.split(",")
geo_string[-2]```
【解决方案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)