Python Pandas 错误
Posted
技术标签:
【中文标题】Python Pandas 错误【英文标题】:Python Pandas error 【发布时间】:2017-11-16 16:44:25 【问题描述】:首先,大家好。我目前正在做一个项目,我的主要目标是从 csv 文件中获取一些地址,计算这些地址与可信数据库的相似性百分比并返回该值。 我有一个可以工作的代码,但是,当我尝试计算相似的速率时,“写入 csv”函数出现错误。代码只运行一行,然后就崩溃了。错误在第 30 行之后开始发生。提前感谢您的帮助,祝您有美好的一天。
完整代码:https://pastebin.com/4sJZJtY9
错误:
Traceback (most recent call last):
File "C:\anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2393, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)
File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)
File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)
KeyError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\Teste.py", line 74, in <module>
cp4[1].to_csv('AI.csv')
File "C:\anaconda\lib\site-packages\pandas\core\frame.py", line 2062, in __getitem__
return self._getitem_column(key)
File "C:\anaconda\lib\site-packages\pandas\core\frame.py", line 2069, in _getitem_column
return self._get_item_cache(key)
File "C:\anaconda\lib\site-packages\pandas\core\generic.py", line 1534, in _get_item_cache
values = self._data.get(item)
File "C:\anaconda\lib\site-packages\pandas\core\internals.py", line 3590, in get
loc = self.items.get_loc(item)
File "C:\anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2395, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)
File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)
File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)
KeyError: 1
【问题讨论】:
第 29 行是注释......我不认为错误存在 【参考方案1】:您正在尝试访问 cp4[1]
,看起来 cp4 是像字典一样的对象并且没有键 1
。无论如何,这有点奇怪。当您迭代 cp4
时,您正在尝试访问 cp4[1]
。请考虑一下。
【讨论】:
【参考方案2】:似乎错误发生在to_csv
方法调用之前:cp4
没有密钥1
。
你读过回溯吗?它明确指出问题出在第 74 行:
File ".\Teste.py", line 74, in <module>
cp4[1].to_csv('AI.csv')
【讨论】:
以上是关于Python Pandas 错误的主要内容,如果未能解决你的问题,请参考以下文章
python pandas dataframe index,错误TypeError:输入必须是可迭代的,pandas版本可能错误