python - '标签 [0] 不在 [索引] 中'

标签 python pandas numpy

当我在 python 中发出以下代码时,其中 myfun 是我的函数名称,B 是 Panda 数据框:

myfun(B,10)

在函数的这一行报错

A=(data.loc[ii]>=A1) & (data.loc[ii]<A2)

其中 B 和数据相同,A1 和 A2 是数字。 错误如下:

'the label [0] is not in the [index]'

我阅读了您网站上的所有内容,但不适用于我的情况。因为没有人解释这个错误在说什么。

谁能告诉我哪里可能有问题,我该如何解决? 甚至说标签 [0] 不在 [index] 中是什么意思?在我的例子中,label[0] 是什么。

最佳答案

来自 pandas 文档:

DataFrame.loc

Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used with a boolean array. Allowed inputs are:

  • A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index).

  • A list or array of labels, e.g. ['a', 'b', 'c'].

  • A slice object with labels, e.g. 'a':'f'.

  • A boolean array of the same length as the axis being sliced, e.g. [True, False, True].

  • A callable function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above)

因为我猜想 ii 是整数类型,所以你需要使用 df.iloc 代替:

A = (data.iloc[ii] >= A1) & (data.iloc[ii] < A2)

关于python - '标签 [0] 不在 [索引] 中',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000584/

相关文章:

python - 如何创建或更新列

python - 'numpy.float6 4' object has no attribute ' 绝对值'

python - 如何对 numpy 矩阵中的每个元素执行操作?

python - 用 SVD 计算 spinv

python - 如何在 Django 中重用函数或类

Python 依赖注入(inject)框架

python - 替换 Pandas Dataframe 中的值

python - pandas 数据框(选择)

python - 在 Python 中从具有多个多元时间序列的数据帧创建数组数组

python - 在 django View 中使用 subprocess.Popen() 执行 python 脚本