Python 如何使用geopy从坐标中获取邮政编码?

Python 如何使用geopy从坐标中获取邮政编码?,python,pandas,numpy,geolocation,geopy,Python,Pandas,Numpy,Geolocation,Geopy,下面是我一直在使用的代码。我是个新手。由于使用API的配额,我一直在测试数据的头部。以下是数据帧的快照: 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 这就是我被绊倒的地方 t

下面是我一直在使用的代码。我是个新手。由于使用API的配额,我一直在测试数据的头部。以下是数据帧的快照:

        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
目前,我发现一个错误:

AttributeError:“Location”对象没有属性“split”


我如何分割位置,以便只提取邮政编码?

如果您只想获取邮政编码,则可以直接访问location对象中的postcode属性

`location.raw['address']['postcode`]

在geopy版本1.21.0中,使用

from geopy.geocoders import Nominatim
我无法使用
location.raw[“address”]
。location.raw的示例如下所示

follows:
{'place_id': 3330757,
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
 'osm_type': 'node',
 'osm_id': 368381231,
 'boundingbox': ['39.6634459', '39.6934459', '-75.6213155', '-75.5913155'],
 'lat': '39.6784459',
 'lon': '-75.6063155',
 'display_name': 'New Castle County Airport, South Hollow Road, Manor Park, New Castle County, Delaware, 19721, United States of America',
 'class': 'aeroway',
 'type': 'aerodrome',
 'importance': 0.6780150946188139,
 'icon': 'https://nominatim.openstreetmap.org/images/mapicons/transport_airport2.p.20.png'}
相反,我用
location.address.split(“,”[-2]
这对我很有用

addressdetails=True
传递给提名以获取此信息()

产生

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'}}

train['Zip'].apply(lambda x:pd.Series(x.split(','))行中的
x
不是可以拆分的字符串。它是一个位置对象。所以您需要使用一个来自Location对象本身的方法。您使用哪个软件包进行地理定位?来自geopy.geocoders导入提名
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'}}