简体   繁体   中英

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:

---------------------------------------------------------------------------
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.

Use iloc function instead of direct index of the dataframe:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM