0

I am trying to run my first ever Python project, so please forgive if this is a silly mistake on my part.

I am trying to create a script that allows me to have a CSV with lat/long pairs and then leverage GoogleMaps API to return me the time and distance between the points.

Based on the error, I am guessing that the issue is that it thinks my latitude values should be strings but are being recognised as integers. Any ideas on what I need to change?

Thanks for any help!

import pandas as pd
import googlemaps
from itertools import tee

#Read CSV file into dataframe named 'df'
df = pd.read_csv('go_track_trackspoints.csv', sep=';')

API_key = 'APIKEY'#enter Google Maps API key
gmaps = googlemaps.Client(key=API_key)



#use pairwise function to be used to iterate through two consecutive rows (pairs) in a data frame
def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

#empty list - will be used to store calculated distances
list = [0]

# Loop through each row in the data frame using pairwise
for (i1, row1), (i2, row2) in pairwise(df.iterrows()):
      #Assign latitude and longitude as origin/departure points
      LatOrigin = row1['Latitude']
      LongOrigin = row1['Longitude']
      origins = (LatOrigin,LongOrigin)

      #Assign latitude and longitude from the next row as the destination point
      LatDest = row2['Latitude']   # Save value as lat
      LongDest = row2['Longitude']  # Save value as lat
      destination = (LatDest,LongDest)

      #pass origin and destination variables to distance_matrix function# output in meters
      result = gmaps.distance_matrix(origins, destination, mode='walking')["rows"][0]["elements"][0]["distance"]["value"]

      #append result to list
      list.append(result)

#Add column 'Distance' to data frame and assign to list values
df['Distance'] = list

df.to_csv('calculated_distances.csv', sep=';', index=None, header= ['id','Latitude','Longitude','track_id','time','distance'])

Here is the error that I am getting:

Traceback (most recent call last):
  File "C:\Users\lumsdena\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3124, in get_value
    return libindex.get_value_box(s, key)
  File "pandas\_libs\index.pyx", line 55, in pandas._libs.index.get_value_box
  File "pandas\_libs\index.pyx", line 63, in pandas._libs.index.get_value_box
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\lumsdena\Documents\Google Maps API\from marty.py", line 27, in <module>
    LatOrigin = row1['Latitude']
  File "C:\Users\lumsdena\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\series.py", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\lumsdena\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3132, in get_value
    raise e1
  File "C:\Users\lumsdena\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexes\base.py", line 3118, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Latitude'
Uri Loya
  • 1,181
  • 2
  • 13
  • 34
Lummers
  • 1
  • 1
  • 1
    Possible duplicate of [TypeError: 'str' object cannot be interpreted as an integer](https://stackoverflow.com/questions/19234598/typeerror-str-object-cannot-be-interpreted-as-an-integer) – mx0 Sep 02 '18 at 11:50
  • 4
    Don't post your Google Maps API key on the Internet. You should change your key immediately. – Tomalak Sep 02 '18 at 14:26

0 Answers0