如何使用geopy从坐标获取邮政编码? [英] How to use geopy to obtain the zip code from coordinates?

查看:80
本文介绍了如何使用geopy从坐标获取邮政编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以下面是我一直在使用的代码.我有点新手.由于使用该API的配额,我一直仅在数据的开头进行测试.下面是数据框的快照:

So below is the code I have been using. I'm a bit of a newb. I've been testing with just the head of the data because of the quota for using the API. Below is a snapshot of the dataframe:

        latitude    longitude
0   -73.99107       40.730054
1   -74.000193      40.718803
2   -73.983849      40.761728
3   -73.97499915    40.68086214
4   -73.89488591    40.66471445

这是我被绊倒的地方.

train['latlng'] = train.apply(lambda row: '{},{}'.format(row['latitude'], 
row['longitude']), axis=1)
train['geocode_data'] = train['latlng'].map(reverse_geocode)
train['Zip'] =train['latlng'].apply(geolocator.reverse)
train['Zip'].apply(lambda x: pd.Series(x.split(',')))
foo = lambda x: pd.Series([i for i in reversed(x.split(','))])
train['Zip']=train['Zip'].apply(lambda x: str(x))
train['Zip']=train['Zip'].apply(foo)[1]

train

此刻,我遇到一个错误:

At the moment, I'm getting an error that:

AttributeError:位置"对象没有属性拆分"

我该如何拆分位置,以便我可以提取邮政编码?

How would I go about splitting up location so that I can just pick up the zip code?

推荐答案

addressdetails = True 传递给Nominatim以获得此信息(

Pass addressdetails=True to Nominatim to get this information (https://geopy.readthedocs.io/en/stable/#nominatim)

location = geolocator.geocode(query={'postalcode':'95119'}, addressdetails=True)
print(location)
location.raw

产生

San Jose, Santa Clara County, California, 95119, United States of America
{'place_id': 237756323,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
 'boundingbox': ['37.072728062914',
  '37.392728062914',
  '-121.94612695296',
  '-121.62612695296'],
 'lat': '37.23272806291426',
 'lon': '-121.78612695295647',
 'display_name': 'San Jose, Santa Clara County, California, 95119, United States of America',
 'class': 'place',
 'type': 'postcode',
 'importance': 0.33499999999999996,
 'address': {'city': 'San Jose',
  'county': 'Santa Clara County',
  'state': 'California',
  'postcode': '95119',
  'country': 'United States of America',
  'country_code': 'us'}}

这篇关于如何使用geopy从坐标获取邮政编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆