我试图使用geopy
确定pandas
数据帧中每一行的国家名称。我所拥有的是:
import pandas as pd
from geopy.geocoders import GoogleV3
df = pd.DataFrame({'ser_no': [1, 1, 1, 2, 2, 2],
'lat': [53.57, 35.52, 35.53, 54.66, 54.67, 55.8],
'lon': [-117.20, -98.29, -98.32, -119.48, -119.47, -119.46]})
def get_country(locations):
locations = geolocator.reverse(row['lat'], row['lon'], timeout = 10)
for location in locations:
for component in location.raw['address_components']:
if 'country' in component['types']:
return component['long_name']
my_key = my_api_key
geolocator = GoogleV3(my_key, proxies ={"http": 'my proxy',
"https": 'my proxy'})
df['country'] = df.apply(lambda row: get_country(row), axis = 1)
这就回来了
^{pr2}$没有发生错误,但是我的输出没有用。我不确定它是返回错误还是我的apply
中有错误。在
^{} 接受字符串,因此需要将函数更改为:
相关问题 更多 >
编程相关推荐