Geocoding is the process of turning an address -- '3020 W Colorado Ave, Colorado Springs, CO 80904' -- into a latitude/longitude coordinate pair. You'd need to do this if you wanted to do some geospatial analysis or make an interactive map or something.
We're going to use the geopy library to geocode a CSV of payday lenders in Illinois. The data file is at data/payday.csv.
Once we have the coordinates, we're going to tack on those columns and write out to a new file, payday-geocoded.csv.
In [ ]:
# import the Google geocoder from geopy
# import Python's csv and time libaries
# Make a geolocator object
# Set the `timeout` keyword argument to 5 (seconds)
# in a `with` block, open the file to read from and the file to write to
# make a DictReader object
# define the headers
# make a DictWriter object
# write headers
# loop over address data
# i'm adding an `enumerate()` function to keep track of where we're at
# so we can kill the loop after 5 iterations --
# that way we don't bombard google and get cut off
# Put the address in a Google-recognizable string: ADDRESS, CITY, STATE ZIP
# Geocode that string
# print the address and results
# write out a new dict
# Before we do all of this with the next row, pause for two seconds.
# once we hit 5, break out of the loop
# let us know when we're done
In [ ]: