'How to resolve problems with pandas in juypter notebook?

I am working my way down through the chapter 3 of book hands on machine learning with scikit learn. I have imported the mnist dataset, but when I am trying to get some image it is showing me an error: screenshot of the error and code! I have import every libraries which I need to import, I have also pushed the code to my GitHub where you can see entire juypter notebook. link to GitHub notebook can anyone help me with this?

the code which I wrote is:

some_digit = X[0]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap=mpl.cm.binary)
plt.axis("off")

save_fig("some_digit_plot")
plt.show()

the error I am getting

KeyError                                  Traceback (most recent call last)
c:\python3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

c:\python3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

c:\python3\lib\site-packages\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)
~\AppData\Local\Temp/ipykernel_5048/2618338264.py in <module>
----> 1 some_digit = X[0]
      2 some_digit_image = some_digit.reshape(28, 28)
      3 plt.imshow(some_digit_image, cmap=mpl.cm.binary)
      4 plt.axis("off")
      5 

c:\python3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   3453             if self.columns.nlevels > 1:
   3454                 return self._getitem_multilevel(key)
-> 3455             indexer = self.columns.get_loc(key)
   3456             if is_integer(indexer):
   3457                 indexer = [indexer]

c:\python3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 0


Solution 1:[1]

I would suggest a different way of resolving this, something that may be helpful in other machine learning projects when you work with Pandas. When slicing data, try to use iloc function instead. In your case, I have tried using X.iloc[0] with the same code and it works, no need to reinstall libraries. And because during preprocessing, as well as splitting data into test/training sets, your indices will get mixed up, therefore calling a row by its label may not be wise.

Solution 2:[2]

Use X.iloc[0] rather than X[0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2 Freddy Mcloughlan