【问题标题】:Pandas KeyError, accessing columnPandas KeyError,访问列
【发布时间】:2021-05-01 19:26:43
【问题描述】:

我正在尝试运行此代码: (这会将 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] 但它也不起作用。

【问题讨论】:

  • 您正在尝试访问名为 0 的列。尝试打印出X.columns 以了解可用的列,并打印X.index 以查看可用的行 - 如果您想访问特定的行 + 列,请使用 - X.loc[row, col]
  • 请发布数据框样本
  • 可以是字符串 '0' 而不是整数 0。试试 x['0']
  • ['0'] 不起作用。 imgur.com/JwE57CQ

标签: python pandas keyerror


【解决方案1】:

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

df.iloc[:, 0]

或按列标题列表访问

df[df.columns[0]]

【讨论】:

  • 感谢它与 df.iloc[0, :] 一起使用,我的错误是,我将列与行混淆了,实际上我必须访问第一行。
  • @Dmitry 然后使用df.iloc[0]
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 2023-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多