Search code examples
pythongeopy

Python geopy accepts manual string but not when looping through csv df


not sure why this happening when i try to get location information. my sample csv has the following

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

and my python file looks like this, i included comments where i am having an issue

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)

aren't these the same?


Solution

  • I solved it like this

    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)
    

    let me know if this is the most efficient way, cheers