I am using Geopy to call different addresses, to see what cities and neighbourhoods they belong to.

from geopy.geocoders import GoogleV3
import json

places = '1238 Davie St, Vancouver, BC'

geolocator = GoogleV3(api_key='')
location = geolocator.geocode(places, language='en')

c = location.raw['address_components']
print c

counties = [c['long_name'] for c in c if 'neighborhood' in c['types']]
localities = [c['long_name'] for c in c if 'locality' in c['types']]
print counties, localities

Its working for counties, but not localities (cities).

[{u'long_name': u'1238', u'types': [u'street_number'], u'short_name': u'1238'}, {u'long_name': u'Davie Street', u'types': [u'route'], u'short_name': u'Davie St'}, {u'long_name': u'Central Vancouver', u'types': [u'neighborhood', u'political'], u'short_name': u'Central Vancouver'}, {u'long_name': u'Vancouver', u'types': [u'locality', u'political'], u'short_name': u'Vancouver'}, {u'long_name': u'Greater Vancouver', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Greater Vancouver'}, {u'long_name': u'British Columbia', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'BC'}, {u'long_name': u'Canada', u'types': [u'country', u'political'], u'short_name': u'CA'}, {u'long_name': u'V6E 1N3', u'types': [u'postal_code'], u'short_name': u'V6E 1N3'}]
[u'Central Vancouver']

---> 32 localities = [c['long_name'] for c in c if 'locality' in c['types']]
     33 print counties,     localities

TypeError: string indices must be integers

Not sure why I am getting that mistake. I also want to be able to pass a Pandas column instead of typing in a static address, is there a neat way of doing that?


Be careful with your variable names. You're dangerously re-defining the variable c within your list comprehensions.

Do this instead:

counties = [x['long_name'] for x in c if 'neighborhood' in x['types']]
localities = [x['long_name'] for x in c if 'locality' in x['types']]

Better yet, use different names entirely so your code is easier to read and reason about.

from geopy.geocoders import GoogleV3

address = '1238 Davie St, Vancouver, BC'

geocoder = GoogleV3(api_key='xyzabc')

location = geocoder.geocode(address, language='en')

address_components = location.raw['address_components']

counties = [addr['long_name'] for addr in address_components if 'neighborhood' in addr['types']]
localities = [addr['long_name'] for addr in address_components if 'locality' in addr['types']]
  • Thank you, it's working now! if instead of a static address I had a pandas column with the address data['address_full'] and I wanted all the value to go through the geocoding and create new columns for counties and localities, how should the code look like? I have tried different ways, mostly with loops and can't seem to make it work – user4718221 Jun 18 '19 at 7:40
  • I recommend asking a new question, probably over on stackoverflow.com. It would be the same as if you were creating any column with the values being determined by a function. So try: 1) create a new column; 2) populate it with a value (any value); 3) populate it with a value from an existing column; 4) populate it as a function using a value from an existing column (e.g. a + 5). Just keep breaking your problem into smaller and smaller pieces until you solve it. – alphabetasoup Jun 18 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.