python - 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] 但它也不起作用。
解决方案
从您的数据框图片中,没有名为 0 的列标题。如果要按索引访问列,可以使用.iloc
主要基于整数位置的列:
df.iloc[:, 0]
或按列标题列表访问
df[df.columns[0]]
推荐阅读
- javascript - react native ios app中无法识别的自定义字体
- java - StreamTokenizer - How to split every character into tokens
- html - style="background: url('')" 仅在 Chrome 中导致网络调用,但在 IE 中没有
- mysql - MySQL在给定范围内按步长查询数据
- reactjs - 如果我们使用 React Redux Hooks,我们如何实现代码重用?
- python - Plotly:如何只显示点击的行?
- c# - 迁移命令后未创建表
- c++ - 递归函数来反转字符串 C++
- node.js - 无法使用 Axios 和 array.map() 获得异步/等待
- php - 如何获取改造 2 发送的服务器上的密钥