Add geo-location data to Pandas data frame

2.6k Views Asked by At

I'm importing a CSV into a pandas data frame, and then I'm trying to create three new columns in that data frame from data retrieved from geopy.geocoders.GoogleV3() :

import pandas from geopy.geocoders import GoogleV3

DATA = pandas.read_csv("file/to/csv") 
geolocator = GoogleV3()

DATA.googleaddress, (DATA.latitude, DATA.longitude) = geolocator.geocode(DATA.address)

Problem is I keep getting this error:

Traceback (most recent call last):
  File "C:/Path/To/GeoCoder.py", line 9, in <module>
    DATA.googleaddress, (DATA.latitude, DATA.longitude) = geolocator.geocode(DATA.address)
TypeError: 'NoneType' object is not iterable

What does this error mean and how do I get around it?

1

There are 1 best solutions below

2
On BEST ANSWER

Because geolocator.geocode expects a single argument at a time, not a list (or array).

You could try:

locs = [ geolocator.geocode(addr) for addr in DATA.address ]
geo_info = pandas.DataFrame(
    [ (addr.address, addr.latitude, addr.longitude) for addr in locs ],
    columns=['googleaddress', 'latitude', 'longitude'])

All you would have to do is merge these DataFrames:

DATA.combine_first(geo_info)

Nnote that it is considered bad form to have an all-uppercase variable in python.