简体   繁体   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)

This code is supposed to read a txt file and then in the next cell plot the points that were contained within the text file, the problem seems to be with reading the txt file, as the plotting works perfectly fine, the following error is given:这段代码应该读取一个 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

Any explanation and assistance would be much appreciated.任何解释和帮助将不胜感激。 This only happens when i run certain text files, and doesn't when I run others.这仅在我运行某些文本文件时发生,而在我运行其他文本文件时不会发生。 At first i thought it might be with:起初我认为它可能与:

dists = float(data1.title[index])

Because it gave the error that data1 doesn't have a title attribute, taking this away gave the above error.因为它给出了 data1 没有 title 属性的错误,把它拿走给出了上述错误。

Use iloc function instead of direct index of the dataframe:使用 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.

相关问题 在处理上述异常的过程中,又发生了一个异常 - During handling of the above exception, another exception occurred 在处理上述异常期间,发生了另一个异常:Python程序 - During handling of the above exception, another exception occurred: Python program KeyError: 1 在处理上述异常的过程中,又发生了一个异常: - KeyError: 1 During handling of the above exception, another exception occurred: 在处理上述异常的过程中,又发生了一个异常:&amp; google sheet not found - During handling of the above exception, another exception occurred: & google sheets not found “在处理上述异常过程中,发生了另一个异常”for循环中的第一个输入 - “ during the handling of above exception, another exception occurred ” for the first input in the for loop 如何解决“在处理上述异常期间,发生了另一个异常:” - How to fix "During handling of the above exception, another exception occurred: " Flask-restful - 在处理上述异常的过程中,发生了另一个异常 - Flask-restful - During handling of the above exception, another exception occurred KeyError:在处理上述异常的过程中,发生了另一个异常 - KeyError: During handling of the above exception, another exception occurred 在处理上述异常的过程中,发生了另一个异常:'Date' - During handling of the above exception, another exception occurred: 'Date' 在处理上述异常期间,发生了另一个异常 - During handling of the above exception, another exception occured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM