2016-04-14 16 views
3

pandas veri çerçevesindeki her satır için ülke adını geopy kullanarak belirlemeyi deniyorum. Ne var olduğunu:pandas veri çerçevesindeki koordinatlardan ülke adını bulmak için geopy kullanarak

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) 

Bu Herhangi bir hata oluşmuş

 lat  lon ser_no          country 
0 53.57 -117.20  1 <function get_country at 0x000000000F6F9C88> 
1 35.52 -98.29  1 <function get_country at 0x000000000F6F9C88> 
2 35.53 -98.32  1 <function get_country at 0x000000000F6F9C88> 
3 54.66 -119.48  2 <function get_country at 0x000000000F6F9C88> 
4 54.67 -119.47  2 <function get_country at 0x000000000F6F9C88> 
5 55.80 -119.46  2 <function get_country at 0x000000000F6F9C88> 

döndürür ama benim çıkış kullanışlı değildir. Yalnızca yanlış bir şekilde dönüp dönmediğimi veya apply numaralı telefonumda yanlış bir şey olup olmadığından emin değilim.

def get_country(row): 
    pos = str(row['lat']) + ', ' + str(row['lon']) 
    locations = geolocator.reverse(pos, timeout = 10) 
    #... rest of func the same 
+0

sizin işlev imzası yerine locations' 'den row'' olmamalıdır: Bu şekilde işlevini değiştirmek gerekir böylece – EdChum

+0

'get_country (konumları)' 'get_country (row)' olmalıdır? – EdChum

+0

Buna nasıl değiştirilir: 'df ['ülke'] = df.apply (lambda satırı: get_country (satır), eksen = 1)' bir fark yaratmamalı ama burada fonksiyon argümanını değiştirerek – EdChum

cevap

İlgili konular