0

I am trying to run this code: (this will download the MNIST dataset to %HOME directory!)

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()

I have this error:

---------------------------------------------------------------------------
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

Code example is from this book: Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow

I tried X.iloc[0] but its also not working.

4
  • 1
    You are trying to access a column named 0. Try printing out X.columns to get an idea of what columns are available, and X.index to see what rows are available - and if you want to access a particular row + column , use - X.loc[row, col] Commented May 1, 2021 at 19:34
  • Pls post a sample of the dataframe Commented May 1, 2021 at 19:34
  • Could it be a string '0' instead of interger 0. Try x['0'] Commented May 1, 2021 at 20:09
  • ['0'] is not working. imgur.com/JwE57CQ Commented May 2, 2021 at 2:33

1 Answer 1

1

From your dataframe pic, there is no column header named 0. If you want to access column by index, you can use .iloc which is primarily integer position based:

df.iloc[:, 0]

Or access by column header list

df[df.columns[0]]
Sign up to request clarification or add additional context in comments.

2 Comments

Thx it works with df.iloc[0, :], my mistake was, that I confused column with row, actually I had to access the first row.
@Dmitry Then use df.iloc[0].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.