簡體   English   中英

關鍵錯誤:1 在處理上述異常的過程中,發生了另一個異常

[英]Key Error: 1 During handling of the above exception, another exception occurred

data1 = pd.read_csv("1-success 1.txt")

record = False
stop = False
distss = []
index = 1
while stop == False:
    dists = float(data1[index])
    index = index + 1
    if dists > 5:
        record = True
        distss.append(dists)
    if record == True and dists <5:
        stop = True
    
len(distss)

這段代碼應該讀取一個 txt 文件,然后在下一個單元格中繪制文本文件中包含的點,問題似乎出在讀取 txt 文件上,因為繪圖工作得很好,給出了以下錯誤:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
D:\Lubertus\Apps\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, 
tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-646e69692594> in <module>
      6 index = 1
      7 while stop == False:
----> 8     dists = float(data1[index])
      9     index = index + 1
     10     if dists > 5:

D:\Lubertus\Apps\Anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

D:\Lubertus\Apps\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, 
tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 1

任何解釋和幫助將不勝感激。 這僅在我運行某些文本文件時發生,而在我運行其他文本文件時不會發生。 起初我認為它可能與:

dists = float(data1.title[index])

因為它給出了 data1 沒有 title 屬性的錯誤,把它拿走給出了上述錯誤。

使用 iloc 函數代替數據幀的直接索引:

data1 = pd.read_csv("1-success 1.txt")

record = False
stop = False
distss = []
index = 1
while stop == False:
    dists = float(data1.iloc[index])
    index = index + 1
    if dists > 5:
        record = True
        distss.append(dists)
    if record == True and dists <5:
        stop = True
    
len(distss)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM