如何使用 python 修复 Geocoder 中的关键错误 0

问题描述

我试图从数据框中的一列地址中绘制出纬度和经度。但它一直给我关键错误 0。

for i in range(len(df['addresses'])):
    g = geocoder.arcgis(df['addresses'][i])
    coordinates.append(tuple(g.latlng))

这是错误信息

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-43-8a4466f30728> in <module>
      1 coordinates = []
      2 for i in range(len(df['addresses'])):
----> 3     g = geocoder.arcgis(df['addresses'][i])
      4     coordinates.append(tuple(g.latlng))

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self,key)
   1069         key = com.apply_if_callable(key,self)
   1070         try:
-> 1071             result = self.index.get_value(self,key)
   1072 
   1073             if not is_scalar(result):

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self,series,key)
   4728         k = self._convert_scalar_indexer(k,kind="getitem")
   4729         try:
-> 4730             return self._engine.get_value(s,k,tz=getattr(series.dtype,"tz",None))
   4731         except KeyError as e1:
   4732             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

解决方法

从 OP 获取更多信息后,此答案已被编辑。

我假设 df 是 Pandas 数据框。

这里的问题是 for i in range(len(df['addresses'])): 在每个循环中都会给你一个整数。然后,您尝试使用该整数从 Pandas 数据框中获取一个列表,这会导致 KeyError

从 for 循环中取出 rangelen,您将获得 addresses 列中的地址。然后将该地址传递给 geocoder

所以,如下:

for address in df['addresses']:
    g = geocoder.arcgis(address)

Documentation of Pandas dataframes

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...