2017-06-14 5 views
-5

まず、みなさんよろしくお願いします。私は現在プロジェクトに取り組んでいます。主な目的は、CSVファイルからいくつかのアドレスを取得し、それらのアドレスが信頼できるデータベースで持っている類似性の割合を計算し、その値を返すことです。 しかし、私は同じ速度を計算しようとすると、私は "csvに書き込む"関数でエラーを取得しようとしているコードを持っている。コードは1行しか実行されず、クラッシュします。エラーは30行目以降に発生します。事前にお手数をおかけして、うれしい一日をお過ごしください。Python Pandasエラー

全コード: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]にアクセスしようとしています。それについてはまだ考えてください。