2017-06-14 107 views
-5

首先,大家好。我目前正在進行一個項目,我的主要目標是從csv文件中獲取一些地址,計算這些地址與可信數據庫的相似百分比並返回該值。 我有一個代碼正在工作,但是當我嘗試計算相似的速率時,我收到「write to csv」函數的錯誤。代碼只運行一行,然後崩潰。錯誤開始發生在第30行之後。預先感謝您的幫助,祝您有美好的一天。Python熊貓錯誤

全碼: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 
+0

第29行是一條評論......我不認爲錯誤在那裏 – depperm

回答

1

看來,錯誤的to_csv方法調用之前發生:cp4沒有關鍵1

你讀過回溯嗎?它明確指出,問題是出在線路74:

File ".\Teste.py", line 74, in <module> 
    cp4[1].to_csv('AI.csv') 
0

你試圖訪問cp4[1],它看起來像CP4就像對象字典,沒有關鍵1。無論如何,這有點奇怪。當您在cp4上迭代時,您正在嘗試訪問cp4[1]。請考慮一下。