【问题标题】:Getting "return self._engine.get_loc(casted_key)" error while working with pandas library使用 pandas 库时出现“return self._engine.get_loc(casted_key)”错误
【发布时间】:2020-09-22 13:07:56
【问题描述】:

我想试试我在互联网上找到的这段代码,

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib

dftrain = pd.read_csv("train.csv", header=0, delimiter=",")  # training data
dfeval = pd.read_csv("eval.csv", header=0, delimiter=",")  # testing data
# print(dftrain)
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')


CATEGORICAL_COLUMNS = ['sec', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERICAL_COLUMNS = ['age', 'fare']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
    vocabulary = dftrain[feature_name].unique()
    feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))

for feature_name in NUMERICAL_COLUMNS:
    feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

不幸的是我总是得到这个错误:

Traceback (most recent call last):
  File "C:\Users\Michael\PycharmProjects\learningTensorv3\venv\lib\site-packages\pandas\core\indexes\base.py", line 2889, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'sec'

(它必须连接到第一个“for”语句,但我自己无法弄清楚,所以我希望有人能帮助我) Stack Overflow 上还有另外 2 个出现此错误的实例,在这两个实例中,它们都只是熊猫行为异常。 我尝试重新安装所有库,检查 IDE 更新(我使用 PyCharm),使用旧版本的库并更改“for”语句,不幸的是没有任何帮助。

【问题讨论】:

    标签: python pandas tensorflow


    【解决方案1】:

    这可能不是您的错误的根本原因,但我遇到了这个 遇到相同错误时发布。

    • 对我来说,根本原因纯粹是我犯了一个疲惫的错误 引用了未正确分配的requests.get 调用。
    • 程序直到几行后我尝试时才出错 在我最初引用的 df 列上执行 transformation api 数据不存在。

    【讨论】:

      猜你喜欢
      • 2023-01-28
      • 1970-01-01
      • 2021-12-31
      • 2013-07-07
      • 2016-10-08
      • 1970-01-01
      • 2019-04-17
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多