[英]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.