【问题标题】:Getting unxpected IndexError when creating a dataframe创建数据框时出现意外的 IndexError
【发布时间】:2022-01-01 19:34:55
【问题描述】:

我正在尝试执行以下代码:

heart_df = pd.read_csv(r"location")
X = heart_df.iloc[:, :-1].values
y = heart_df.iloc[:, 11].values

new_df = X[["Sex", "ChestPainType", "RestingECG", "ExerciseAngina", "ST_Slope"]].values() #this is line 17

cat_cols = new_df.copy()

并得到 IndexError 如下:

  File "***location***", line 17, in <module>
  new_df = X[["Sex", "ChestPainType", "RestingECG", "ExerciseAngina", "ST_Slope"]].values()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

据我所知,当我们使用浮点数作为索引时会出现这个 IndexError,但不明白为什么会出现这种情况。

在这里,通过创建 new_df 和 cat_cols,我想将分类列分开以在稍后阶段应用 OneHotEncoding。

数据集在这里:https://www.kaggle.com/fedesoriano/heart-failure-prediction

【问题讨论】:

    标签: python-3.x one-hot-encoding index-error


    【解决方案1】:

    错误来自:

    X = heart_df.iloc[:, :-1].values
    

    .values 部分将数据框转换为 numpy 数组,X 中的某些列与 numpy 数组不兼容。

    所以我们可以这样写:

    X = heart_df.iloc[:, :-1]
    new_df = X[["Sex", "ChestPainType", "RestingECG", "ExerciseAngina", "ST_Slope"]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多