I used this script to do some batch geocoding from .csv. This requires that one column contains the full text address that you want to geocode, and one column called "UniqueID", which has a unique identifier for each element in the CSV. It also prints a list of addresses that geocoding failed. It also performs a quick check to see if the zip code may be incorrect / throw geocoding:
def main(path, filename): # path to where your .csv lives, and the name of the csv. import geopy from geopy.geocoders import ArcGIS import pandas as pd Target_Addresses = pd.read_csv(path+'\\'+filename) Target_Addresses['Lat'] = np.nan Target_Addresses['Long'] = np.nan Indexed_Targets = Target_Addresses.set_index('UniqueID') geolocator = ArcGIS() #some parameters here Fails = [] for index, row in Indexed_Targets.iterrows(): Address = row['Address'] Result = geolocator.geocode(Address) if Result == None: Result = geolocator.geocode(Address[:-7]) if Result == None: Fails.append[Address] else: Indexed_Targets.set_value(index, 'Lat', Result.latitude) Indexed_Targets.set_value(index, 'Long', Result.longitude) else: Indexed_Targets.set_value(index, 'Lat', Result.latitude) Indexed_Targets.set_value(index, 'Long', Result.longitude) for address in Fails: print address Indexed_Targets.to_csv(filename[:-4]+"_RESULTS.csv") if __name__ == '__main__': main(path, filename) # whatever these are for you...
This will output a new csv with "_RESULTS" (for example, the input "addresses.csv" will output "addresses_RESULTS.csv") with two new columns for "Lat" and "Long".
source share