1

I currently have a data frame that has address details of certain place. I want to use Google geocode API key to find the coordinates - Latitude and Longitude in order to plot a map. Does anybody know how to do this? I have tried the below code but it is returning 'Error, skipping address...' on all the lines of addresses.

I would greatly appreciate any help!

import pandas as pd
import os
from geopy import geocoders
from geopy.geocoders import GoogleV3

API_KEY = os.getenv("API1234")
g = GoogleV3(api_key=API_KEY)


loc_coordinates = []
loc_address = []

for address in df.Address:
    try:
        inputAddress = Address
        location = g.geocode(inputAddress, timeout=15)
        loc_coordinates.append((location.latitude, location.longitude))
        loc_Address.append(inputAddress)
    except:
        print('Error, skipping address...')


df_geocodes = pd.DataFrame({'coordinate':loc_coordinates,'address':loc_address})
Harry
  • 41
  • 3
  • 7
  • Two things up front: 1. This is possibly just a copy-paste error but you've written ```inputAddress = Address``` when I suppose you meant ```inputAddress = address```, so as to not assign a type to that variable but the variable of your for-loop. 2. instead of printing 'Error, skipping address...' in the except clause, try catching the actual exception, which might give you more of a hint what is going wrong (I bet it's a TypeError in the line after the inputAddress assignment). You would do ```except Exception as e:``` and then print infos about the actual exception. – Jan Nash Nov 14 '17 at 10:37
  • Also see [this question](https://stackoverflow.com/questions/18982610/difference-between-except-and-except-exception-as-e-in-python) for further reference. – Jan Nash Nov 14 '17 at 10:38

1 Answers1

2

You had some typos: Address instead of address, loc_Address instead of loc_address.

But what is df.Address ?

Try this:

import pandas as pd
import os
from geopy import geocoders
from geopy.geocoders import GoogleV3

API_KEY = os.getenv("API1234")
g = GoogleV3(api_key=API_KEY)


loc_coordinates = []
loc_address = []
for address in df.Address:
    try:
        inputAddress = address
        location = g.geocode(inputAddress, timeout=15)
        loc_coordinates.append((location.latitude, location.longitude))
        loc_address.append(inputAddress)
    except Exception as e:
        print('Error, skipping address...', e)


df_geocodes = pd.DataFrame({'coordinate':loc_coordinates,'address':loc_address})
Robert D. Mogos
  • 900
  • 7
  • 14
  • Thank you for this! df.Address df is my dataframe that I am working on and Address is the column in which I am trying to get the Latitude and Longitude – Harry Nov 14 '17 at 10:32
  • I have declared it in the row above import pandas as pd import numpy as np df.Address = pd.read_csv("") – Harry Nov 14 '17 at 10:36
  • I am still however getting 'Error, Skipping Address' once I run the code with the given corrections – Harry Nov 14 '17 at 10:37
  • Can you add a `print(df.Address)` before the for loop and show me what it says ? – Robert D. Mogos Nov 14 '17 at 10:38
  • I will try that now – Harry Nov 14 '17 at 10:39
  • It prints out the below statement Empty DataFrame Columns: [address, coordinate] Index: [] Error, skipping address... – Harry Nov 14 '17 at 10:40
  • OK, so the problem is that you don't have any addresses in `df.Address`. Probably you are not reading correctly the csv – Robert D. Mogos Nov 14 '17 at 10:42
  • If you replace `for address in df.Address:` with `for address in `["55 Rue d'Amsterdam, Paris"]:` for example and you add a `print(df_geocodes)` at the end, you will see the address and the coordinates – Robert D. Mogos Nov 14 '17 at 10:43
  • I have re ran the code that calls the dataframe and it now prints all the addresses however once I remove the statement print(df.Address) I'm getting Error, Skipping address @Robert D. Mogos – Harry Nov 14 '17 at 10:45
  • once I done that it printed address coordinate 0 Address2 (41.088137, -81.544786) 1 Postcode (53.8074595, -1.524806) 2 LCG (39.047906, -77.1206027) – Harry Nov 14 '17 at 10:47
  • I updated the answer, can you copy that code and show the new output? – Robert D. Mogos Nov 14 '17 at 10:50
  • I have tried that and it is printing out my whole data frame and after the 339 rows and columns that it prints it states: ('Geoloc ', 'PracNo') ('Geoloc ', 'PracticeName' ('Error, skipping address...', GeocoderQuotaExceeded('The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time.',)) – Harry Nov 14 '17 at 10:56
  • OK, so that's your problem. You are exceeding your query quota. You need a key that allows you to search addresses more often. Did you sign up for an API key on google ? – Robert D. Mogos Nov 14 '17 at 11:03
  • You need to sign up here and get a key https://developers.google.com/maps/documentation/geocoding/start (Click on `Get a key` on the top right). After you get the key, replace `API_KEY = os.getenv("API1234")` with `API_KEY = "YOUR GOOGLE KEY"` – Robert D. Mogos Nov 14 '17 at 11:06
  • It still is coming out with an error ('Geoloc ', 'PracNo') ('Error, skipping address...', NameError("name 'address' is not defined",)) ('Geoloc ', 'PracticeName') ('Error, skipping address...', NameError("name 'address' is not defined",)) ('Geoloc ', 'Address1') ('Error, skipping address...', NameError("name 'address' is not defined",)) ('Geoloc ', 'Address2') ('Error, skipping address...', NameError("name 'address' is not defined",)) ('Geoloc ', 'Address3') ('Error, skipping address...', NameError("name 'address' is not defined",)) ('Geoloc ', 'Postcode') – Harry Nov 14 '17 at 11:15
  • Just by adding the new key you get this or did you change something else ? – Robert D. Mogos Nov 14 '17 at 11:46