0

I'm doing an assignment at the university. I`m using jupyter notebook.

import warnings
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import numpy as np
import matplotlib.pyplot as plt

warnings.filterwarnings("ignore")
from sklearn.feature_selection import SelectKBest,f_classif
from sklearn.decomposition import PCA

url = \
    "http://archive.ics.uci.edu/ml/"+\
    "machine-learning-databases/wine/wine.data"

x = pd.read_csv(url)

print (x)
x[0] = pd.Series(pd.cut(x[0],5))
x[0] = lb_make.fit_transform(0).astype(int)
x[0]

As a result, I get an error

KeyError                                  Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py:3629, in Index.get_loc(self, key, method, tolerance)
   3628 try:
-> 3629     return self._engine.get_loc(casted_key)
   3630 except KeyError as err:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0


How can I fix it? What can you recommend for studying this topic?

  • 1
    Welcome to stack overflow! Please look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [edit] your question to include a [mcve] showing a sample of your input data and expected output based on that input. The error is telling you that your dataframe does not have a column ("key") called `0` at the point where the error occurs. You may need to use `.loc` or `.iloc` to call a specific column or row, depending on what you're trying to do – G. Anderson Sep 09 '22 at 21:49
  • what is `lb_make`? What you try to do with `.fit_transform(0)` ? And what you try to do with `x[0]`? Shouldn't be `x.loc[0]` which means `x.loc[row_index]` because `x[0]` means `x[column_name]` – furas Sep 10 '22 at 15:53

0 Answers0