Python 解读a“;回溯(最近一次呼叫最后一次):&x201D;错误

Python 解读a“;回溯(最近一次呼叫最后一次):&x201D;错误,python,pandas,numpy,dataframe,compiler-errors,Python,Pandas,Numpy,Dataframe,Compiler Errors,我意识到这个问题以前已经解释过很多次了,所以我理解这是否是一个重复的问题,但我有一些更理论的问题要问,这可能证明这是一个新问题。我不熟悉Python(等等),所以请容忍我 我正在尝试读取一个.csv文件,该文件有16列和30000行,填充值从0到17。没有空的单元格。我想做的是遍历每一行,对每一行的单元格进行逐项减法。目前,我正试图使用熊猫数据帧来实现这一点。所以我的第一个问题是:我应该使用不同的数据结构吗?我已经读到数据帧不适合遍历行 接下来,关于标题问题,我需要帮助解释我的错误。Thusfa

我意识到这个问题以前已经解释过很多次了,所以我理解这是否是一个重复的问题,但我有一些更理论的问题要问,这可能证明这是一个新问题。我不熟悉Python(等等),所以请容忍我

我正在尝试读取一个.csv文件,该文件有16列和30000行,填充值从0到17。没有空的单元格。我想做的是遍历每一行,对每一行的单元格进行逐项减法。目前,我正试图使用熊猫数据帧来实现这一点。所以我的第一个问题是:我应该使用不同的数据结构吗?我已经读到数据帧不适合遍历行

接下来,关于标题问题,我需要帮助解释我的错误。Thusfar,我只写了一些代码来尝试对一小部分数据进行减法。这是我的密码:

import numpy as np
import pandas as pd
这一切都按预期进行。但是,当我将最后一段代码更改为以下内容时,会出现一个错误:

for i in range(15):
    print (df[i][0]-df[i][1])
我将在下面发布错误的记录。即使我有一个可用的代码,我也尝试这样做的原因是,当我编写完整的脚本时,我正在迭代已知数量的行。值得一提的是,我正在Jupyter online上做这件事



keyrerror回溯(最近一次调用)
/get_loc中的srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2889尝试:
->2890自动返回发动机。获取锁定位置(钥匙)
2891键错误除外:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
关键错误:0
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
1表示范围(15)内的i:
---->2打印(df[i][0]-df[i][1])
/srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/frame.py in_u____getitem_u____(self,key)
2973如果self.columns.nlevels>1:
2974返回自我。\u获取项目\u多级(键)
->2975索引器=self.columns.get_loc(键)
2976如果是_整数(索引器):
2977索引器=[索引器]
/get_loc中的srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2890自动返回发动机。获取锁定位置(钥匙)
2891键错误除外:
->2892返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2893 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
2894如果indexer.ndim>1或indexer.size>1:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.PyObjectHashTable.get_item()中
关键错误:0

我将展开我的评论,回答最初的问题-解释异常


错误的原因是,您的数据帧很可能没有使用整数作为列名,因此整数0到15将导致您看到的KeyError,这是两个异常的最后一行:KeyError:0

在回溯中,Python为您提供了发生错误的附加上下文

当您尝试访问数据帧的列
0
时,处理代码到达函数
get_loc()
base.py
的第2890行

在该代码中,发生的
KeyError
由包含
try/except
的处理。但是,处理调用也会引发一个未处理的
KeyError
(不幸的是,此调用也未包含在回溯中)。这就是“
在处理上述异常时,出现另一个异常:
”消息的地方

要说明如何使用代码本身,请执行以下操作:

            ...
            try:
                return self._engine.get_loc(key) # <- KeyError raised here
            except KeyError:                     # <- Caught by except
                return self._engine.get_loc(self._maybe_cast_indexer(key)) # <- 2nd KeyError
            ...

错误的原因是数据帧很可能没有使用整数作为列名,因此整数0到15将导致您看到的
KeyError
,这是这两个异常的最后一行:
KeyError:0
@b_c我尝试将列名更改为整数0到15,但仍然出现此错误。我直接在.csv中更改了它们,而不是在Python代码中。因为您是从csv读取的,所以它们很可能被读取为字符串(即,“0”、“1”而不是仅0、1等)
for i in range(15):
    print (df[i][0]-df[i][1])
KeyError                                  Traceback (most recent call last)
/srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2889             try:
-> 2890                 return self._engine.get_loc(key)
   2891             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: 0

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-0faa876fbe56> in <module>
      1 for i in range(15):
----> 2     print (df[i][0]-df[i][1])

/srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2973             if self.columns.nlevels > 1:
   2974                 return self._getitem_multilevel(key)
-> 2975             indexer = self.columns.get_loc(key)
   2976             if is_integer(indexer):
   2977                 indexer = [indexer]

/srv/conda/envs/notebook/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2890                 return self._engine.get_loc(key)
   2891             except KeyError:
-> 2892                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2893         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2894         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: 0
            ...
            try:
                return self._engine.get_loc(key) # <- KeyError raised here
            except KeyError:                     # <- Caught by except
                return self._engine.get_loc(self._maybe_cast_indexer(key)) # <- 2nd KeyError
            ...
KeyError: 0