Stackcans
Stackcans

Reputation: 351

Tuple over 2 columns on a dataframe to reverse geocode

I'm using googlemaps to reverse-geocode some coordinates into addresses, however I would like to know which column these addresses belong to relative to the coordinates row in the pandas dataframe. So, I have decided to set up this code:

dict = {'Location Name': {0: 'Hahnville',
  1: 'Home Place Plantation',
  2: 'New Orleans',
  3: "1117 Broadway (Gil's Music Shop)",
  4: "2715 North Junett St (Kat and Bianca's House)"},
 'Latitude': {0: 29.976858,
  1: 29.971119,
  2: 29.950888,
  3: 47.252495,
  4: 47.272591},
 'Longitude': {0: -90.410561,
  1: -90.407745,
  2: -90.076546,
  3: -122.439644,
  4: -122.47448},
 'movie': {0: '10-cloverfield-lane-locations-553',
  1: '10-cloverfield-lane-locations-553',
  2: '10-cloverfield-lane-locations-553',
  3: '10-things-i-hate-about-you-locations-250',
  4: '10-things-i-hate-about-you-locations-250'}}

test = pd.DataFrame(dict)

responses = []
for i, row in test.iterrows():
   response = gmaps.reverse_geocode(tuple(row.Latitude, row.Longitude))
   responses.append(response)

test['response'] = responses

However I get the following error:

TypeError: tuple expected at most 1 argument, got 2

Upvotes: 0

Views: 114

Answers (1)

Zaid Al Shattle
Zaid Al Shattle

Reputation: 1534

You are using tuple(row.Latitude, row.Longitude), but an easier way is to just have (row.Latitude, row.Longitude), as by definition that's a tuple. and that should remove the error you are getting. E.g. your line will be:

response = gmaps.reverse_geocode((row.Latitude, row.Longitude))

The function tuple only takes one parameter, which needs to be an iterable. Which is why you were getting the error there.

Edit: For the error, if for some reason the above didn't work, then this should work, assuming your row.Latitude and Longitude values are valid:

response = gmaps.reverse_geocode({"lat" : row.Latitude,"lng" row.Longitude})

Upvotes: 1

Related Questions