Harry
Harry

Reputation: 51

Geocoding in python get Latitude and Longitude from addresses using API key

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})

Upvotes: 1

Views: 8764

Answers (1)

Robert D. Mogos
Robert D. Mogos

Reputation: 910

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})

Upvotes: 2

Related Questions