如何解决谷歌反向地理编码API通过熊猫数据帧迭代的问题



我正在尝试使用Google的反向地理编码API来获取250个纬度和经度坐标列表的城市、州和国家。pandas数据帧df包含df['point']列中的位置坐标。我想将城市、州和国家作为新列添加到原始df中。下面的python代码非常适用于state和country列,但对于city列则失败,因为"city_list"缺少两个结果。我得到这个错误:

ValueError: Length of values (248) does not match length of index (250)

我一直在努力想办法解决这个问题。有没有办法加上";错误";两排都没有产生城市?非常非常感谢您的帮助!!!

import googlemaps
import json
import pandas as pd
gmaps = googlemaps.Client(key='APIKEYHERE')
stored=[]
city_list=[]
state_list=[]
country_list=[]
for latlng in df['point']:
r_geocode_result = gmaps.reverse_geocode((latlng))
stored.append(r_geocode_result)
address_components = r_geocode_result[0]['address_components']
for address_type in address_components:
flags = address_type.get('types', [])
if 'locality' in flags:
city = address_type['long_name']
city_list.append(city)
elif 'administrative_area_level_1' in flags:
state = address_type['short_name']
state_list.append(state)
elif 'country' in flags and 'political' in flags:
country = address_type['short_name']
country_list.append(country)
# Convert lists into columns in original df
df['city'] = city_list
df['state'] = state_list
df['country'] = country_list

显然,其中一个创建的列表比数据帧短。之所以会发生这种情况,是因为你只有if条件,而没有其他条件。因此,如果不满足if条件,则代码不会附加任何内容。作为一种解决方案,您可以通过列表理解来查找值,并在列表为空时将None分配给该值。此外,我建议使用pd.apply:

import googlemaps
import pandas as pd
gmaps = googlemaps.Client(key='APIKEYHERE')
def get_location(latlng):
r_geocode_result = gmaps.reverse_geocode((latlng))
address_components = r_geocode_result[0]['address_components']
city = [i['long_name'] for i in address_components if 'locality' in i['types']]
city = city[0] if city else None
state = [i['long_name'] for i in address_components if 'administrative_area_level_1' in i['types']]
state = state[0] if state else None
country = [i['long_name'] for i in address_components if all(elem in ['country', 'political'] for elem in i['types'])]
country = country[0] if country else None
return pd.Series([city, state, country])
df[['city','state','country']] = df['point'].apply(get_location)

最新更新