【问题标题】:Trying to conditionally sum some element in a dataframe, I obtain KeyError: "None of [Float64Index(...)] are in the [columns]"尝试有条件地对数据框中的某些元素求和,我得到 KeyError: "None of [Float64Index(...)] are in the [columns]"
【发布时间】:2019-10-15 10:14:50
【问题描述】:

我有一个数据框,df

在循环中,每次迭代我都会创建此 Dataframe temp_df 的副本。几乎一样,只是略有不同。

我有一个分类模型,modelclassification_results 函数返回一个由 0 和 1 组成的数组,它告诉我 temp_df 的每个元素的预测类别

我想获取 temp_df 的所有预测类别 = 1 的元素,并将这些确切元素的值的总和存储在另一列中。

整个代码是这样的:

for c in range(number_of_iterations):
    temp_df = df.copy()
    temp_df["my_column"] = df.apply(lambda row: my_function(row, c), axis=1)
    results[c] = temp_df[classification_results(model, temp_df)].another_column.sum()

地点:

classification_results(model, temp_df)

返回类似:

array([1., 0., 1., 1., 0., 1., 0.])

所以我希望:

temp_df[classification_results(model, temp_df)]

返回一个只有来自 temp_df 的几个元素的 df,就像我在做类似的事情:

temp_df[temp_df["a_column"] == Something]

相反,运行我获得的代码:

KeyError: "None of [Float64Index([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
...
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
dtype='float64', length=761)] are in the [columns]"

我找不到答案。我做错了什么?

【问题讨论】:

  • 试试:results[c] = temp_df[classification_results(model, temp_df) == 1].another_column.sum()
  • @Aryerez 它有效,您可以直接发布它作为答案
  • 没有足够的工作来回答:)

标签: python pandas indexing keyerror


【解决方案1】:

根据@Aryerez 的评论,我替换了

temp_df[classification_results(model, temp_df)]

temp_df[classification_results(model, temp_df) == 1]

(这也很好:)

temp_df[classification_results(model, temp_df).astype(bool)]

因为您不能使用浮点数组(甚至不能使用 Int 数组,这让我感到惊讶)来过滤 DF 中的数据

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 2022-12-12
    • 1970-01-01
    • 2021-04-01
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2019-06-27
    相关资源
    最近更新 更多