【问题标题】:Getting "return self._engine.get_loc(casted_key)" error获取“返回 self._engine.get_loc(casted_key)”错误
【发布时间】:2023-01-28 18:00:17
【问题描述】:

我在大学做作业。我正在使用 jupyter 笔记本。

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]

结果,我得到一个错误

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 to make good pandas examples 和 edit 您的问题,以包含 minimal reproducible example 显示您的输入数据样本和基于该输入的预期输出。该错误告诉您您的数据框在发生错误时没有名为 0 的列(“键”)。您可能需要使用 .loc 或 .iloc 来调用特定的列或行,具体取决于您要执行的操作
  • lb_make 是什么?你想用.fit_transform(0)做什么?你想用x[0]做什么?不应该是x.loc[0],这意味着x.loc[row_index],因为x[0]意味着x[column_name]

标签: python python-3.x pandas dataframe


【解决方案1】:

hehe 得到了同样的错误,你想出来了吗

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多