如何使用python修复Geocoder中的关键错误0
我试图从一个数据帧中的一列地址映射出纬度和经度。但它一直给我关键错误0如何使用python修复Geocoder中的关键错误0,python,pandas,keyerror,geocoder,Python,Pandas,Keyerror,Geocoder,我试图从一个数据帧中的一列地址映射出纬度和经度。但它一直给我关键错误0 for i in range(len(df['addresses'])): g = geocoder.arcgis(df['addresses'][i]) coordinates.append(tuple(g.latlng)) 这是错误消息 --------------------------------------------------------------------------- KeyError
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
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在里面
1坐标=[]
2表示范围内的i(len(df['addresses']):
---->3g=geocoder.arcgis(df['addresses'][i])
4坐标。追加(元组(g.latlng))
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py in\uuuuu getitem\uuuuuu(self,key)
1069 key=com.apply\u如果可调用(key,self)
1070尝试:
->1071结果=self.index.get_值(self,key)
1072
1073如果不是标量(结果):
get_值中的~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/index/base.py(self、series、key)
4728 k=self.\u convert\u scalar\u indexer(k,kind=“getitem”)
4729尝试:
->4730返回self._engine.get_值(s,k,tz=getattr(series.dtype,“tz”,None))
4731除键错误为e1外:
4732如果len(self)>0且(self.holds_integer()或self.is_boolean()):
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
关键错误:0
从OP获得更多信息后,此答案已被编辑
我假设df
是一个数据帧
这里的问题是对于范围内的i(len(df['addresses']):
为每个循环提供一个整数。然后,您试图从Pandas数据帧中获取一个包含该整数的列表,这将导致出现keyrerror
从for循环中取出范围
和len
,您将从地址
列中获得一个地址。然后将该地址传递给geocoder
因此,情况如下:
df['addresses']中地址的:
g=地理编码器.arcgis(地址)
您能复制整个错误消息吗?我编辑了它。谢谢,很有趣。但该专栏并非词典。你能解释一下吗?它返回错误类型错误:“numpy.ndarray”对象是不可调用的哦,对了,我完全没有注意到我们在这里看到的是熊猫数据帧。嗯,在df['addresses']中为i取
怎么样?不,还是不行。它只返回“KeyError:'3315 spenard road,anchorage”您是否从第3行删除了[i]
<代码>用于df['addresses']中的地址:g=geocoder.arcgis(地址)