首页 > 解决方案 > Pandas KeyError,访问列

问题描述

我正在尝试运行此代码:(这会将 MNIST 数据集下载到 %HOME 目录!)

from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
mnist.keys()
X, y = mnist["data"], mnist["target"]

import matplotlib as mpl
import matplotlib.pyplot as plt
some_digit = X[0] # **ERROR LINE** <---------
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap = mpl.cm.binary, interpolation="nearest")
plt.axis("off")
plt.show()

我有这个错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

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

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-45-d5d685fca2de> in <module>
      2 import matplotlib.pyplot as plt
      3 import numpy as np
----> 4 some_digit = X[0]
      5 some_digit_image = some_digit.reshape(28, 28)
      6 plt.imshow(some_digit_image, cmap = mpl.cm.binary, interpolation="nearest")

~/.local/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   3022             if self.columns.nlevels > 1:
   3023                 return self._getitem_multilevel(key)
-> 3024             indexer = self.columns.get_loc(key)
   3025             if is_integer(indexer):
   3026                 indexer = [indexer]

~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 0

代码示例来自本书:Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow

我试过 X.iloc[0] 但它也不起作用。

标签: pythonpandaskeyerror

解决方案


从您的数据框图片中,没有名为 0 的列标题。如果要按索引访问列,可以使用.iloc主要基于整数位置的列:

df.iloc[:, 0]

或按列标题列表访问

df[df.columns[0]]

推荐阅读