无法理解大Pandas在Jupyter Noteb引起的关键错误

2024-09-30 04:39:45 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是代码的一部分,其中有一个注意到的问题。我想问题可能是熊猫更新引起的,因为很久以前一切似乎都很好。泰

阅读课堂成绩

df = pd.read_csv('data/student-mat.csv')

过滤掉0的分数

^{pr2}$

日志:

KeyError                                  Traceback (most recent call last)
~\Anaconda51\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             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: 'G3'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-4-2948d93a326d> in <module>()
      3 
      4 # Filter out grades that were 0
----> 5 df = df[~df['G3'].isin([0, 1])]
      6 
      7 df = df.rename(columns={'G3': 'Grade'})

~\Anaconda51\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~\Anaconda51\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~\Anaconda51\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~\Anaconda51\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~\Anaconda51\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: 'G3'

Tags: keyinselfpandasdfgetindexlib
1条回答
网友
1楼 · 发布于 2024-09-30 04:39:45

第二次执行笔记本时,你不再有“G3”,因为它现在是“Grade”。在

您需要做的是使您的单元保持一致,而不是编写它们,以便一个单元内的代码依赖于前一个单元的状态。在

例如,在本例中,从数据集中删除“G3”的单元格应该是一个单独的单元格,这样您就知道在这个单元格之后,您就不能再使用G3了。在

相关问题 更多 >

    热门问题