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

2024-03-29 04:54:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个数据帧中的一列地址映射出纬度和经度。但它一直给我关键错误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

Tags: keyinselfpandasdfgetindexlen
1条回答
网友
1楼 · 发布于 2024-03-29 04:54:22

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

我假设df是一个数据帧

这里的问题是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

相关问题 更多 >