首页 > 解决方案 > Python:Google Maps API,CSV 文件上的 TypeError

问题描述

我正在尝试运行我的第一个 Python 项目,如果这是我的一个愚蠢的错误,请原谅。

我正在尝试创建一个脚本,允许我拥有一个带有纬度/经度对的 CSV,然后利用 GoogleMaps API 向我返回点之间的时间和距离。

根据错误,我猜测问题在于它认为我的纬度值应该是字符串,但被识别为整数。关于我需要改变什么的任何想法?

谢谢你的帮助!

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'])

这是我得到的错误:

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'

标签: pythonpandastypeerror

解决方案


推荐阅读