当我在 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/