My data set comes from http://book.flowingdata.com/ch08/geocode/costcos-limited.csv
import googlemaps import pandas as pd #get a key here https://console.cloud.google.com/google/maps-apis/start g_api_key = 'YOUR API KEY HERE' #register your key gmaps = googlemaps.Client(key=g_api_key) #import the csv file from http://book.flowingdata.com/ch08/geocode/costcos-limited.csv #only 5 columns should exist, if you get an error saying more do then select the next 5 columns and delete them df = pd.read_csv('datasources/costcos-limited.csv', skiprows = 0) #create two new columns for the lat and long in your DataFrame df["latitude"] = "" df["longitude"] = "" #iterate over your dataframe for index, row in df.iterrows(): #pass in an address, in my case I am passing Address, City, State #the more accurate you pass in the more likely your results is accurate location = gmaps.geocode(row + ", " + row + ", " + row) #write the new values to your DataFrame df.loc[index, 'latitude'] = location['geometry']['location']['lat'] df.loc[index, 'longitude'] = location['geometry']['location']['lng'] #create a new dataframe from your list of lists #save the dataframe to back to your csv df.to_csv("datasources/costcolocations.csv", sep=',',index=False) print(df)
A Web Developer by trade, future data scientist.
A motorcycle enthusiast at heart.
Most days I’d rather be in the woods anywhere.