Google Geocode ValueError

Viewed 23

I'm currently trying to pull the latitude and longitude values from a series of addresses that are currently stored in a pandas dataframe. I have the following code currently:

import pandas as pd
from googlemaps import Client as GoogleMaps

GoogleAPIkey = "myAPIkey"
gmaps = GoogleMaps(GoogleAPIkey)

df = pd.read_csv("addresslist")

df output is as follows:

 |Location                            |
0|Robert Pitt Drive, Spring Valley, NY|
1|Bryant Avenue, Harrison, NY         |

Ideally I'd like to have the following output:

 |Location                            | Lat | Lon |
0|Robert Pitt Drive, Spring Valley, NY|     |     | 
1|Bryant Avenue, Harrison, NY         |     |     | 

I'm using the following code to create a new column in the df:

df["geocode_result"] = gmaps.geocode(df["Location"])

However, I get a ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Do I need to convert my string series to another format to get the right value?

Thanks

1 Answers

Try changing your code to this:

df["geocode_result"] = df['Location'].map(lambda x: gmaps.geocode(x))

Method .map() or .apply() allows you to apply gmaps.geocode() function to each value stored in df['Location'] column.

Related