【问题标题】:How do I fix the key error 0 in Geocoder with python如何使用 python 修复 Geocoder 中的关键错误 0
【发布时间】:2021-03-23 21:08:57
【问题描述】:

我正在尝试从数据框中的一列地址中绘制出纬度和经度。但它一直给我关键错误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

【问题讨论】:

  • 你能复制整个错误信息吗?
  • 我编辑了它。谢谢

标签: python pandas keyerror geocoder


【解决方案1】:

在从 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

【讨论】:

  • 嗯,很有趣。但该列不是字典。你能解释一下吗?
  • 返回错误 TypeError: 'numpy.ndarray' object is not callable
  • 哦,对了,我完全错过了我们在这里查看 pandas 数据帧的情况。嗯,只拿for i in df['addresses'] 怎么样?
  • no 还是不行。它只是返回 'KeyError: '3315 spenard road, anchorage'
  • 您是否从第 3 行删除了 [i]for address in df['addresses']: g = geocoder.arcgis(address)
猜你喜欢
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
相关资源
最近更新 更多