2017-02-16 45 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) 

让我知道,如果这是最有效的方式,欢呼声