2017-02-16 41 views
0

不知道爲什麼會發生這種情況,當我嘗試獲取位置信息。我的樣本CSV具有以下Python geopy接受手動字符串,但不通過CSV循環時df

address,city,state,zip 
767 5th Ave, New York, NY, 10153 

和我的Python文件看起來像這樣,我在那裏我有一個問題

from geopy.geocoders import Nominatim 
geolocator = Nominatim() 
import pandas as pd 

df = pd.read_csv('test.csv') 

for index, row in df.iterrows(): 
    # doesnt work when looping 
    location = geolocator.geocode(row['address'],row['city'],row['state'],row['zip']) 

    # works manually 
    #location = geolocator.geocode("767 5th Ave, New York, NY, 10153") 
    print(location.raw) 

不是這些同樣包括評論?

回答

0

我解決它像這樣

for index, row in df.iterrows(): 
    street = str(row['address']) 
    city = str(row['city']) 
    state = str(row['state']) 
    zipcode = str(row['zip']) 
    location = geolocator.geocode("%s %s %s %s" % (street,city,state,zipcode)) 
    print(location.raw) 

讓我知道,如果這是最有效的方式,歡呼聲