pandas '标签 [0] 不在 [索引] 中'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51000584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'the label [0] is not in the [index]'
提问by justin
when I issue following code in python where myfun is the name of my function, B is Panda data frame:
当我在 python 中发出以下代码时,其中 myfun 是我的函数名称,B 是 Panda 数据框:
myfun(B,10)
it gives error at this line in the function
它在函数的这一行给出错误
A=(data.loc[ii]>=A1) & (data.loc[ii]<A2)
where B and data are the same and A1 and A2 are numbers. The error is as follow:
其中 B 和数据相同,A1 和 A2 是数字。错误如下:
'the label [0] is not in the [index]'
I read everything in your website it does not apply to my case. as nobody has explained what this error is talking about.
我阅读了您网站上的所有内容,但不适用于我的情况。因为没有人解释过这个错误在说什么。
Can anyone tell where possibly I can have a problem and how I can fix it? What does even mean to say label [0] is not in [index]? what is label[0] in my case.
谁能告诉我哪里可能有问题以及如何解决?甚至说标签 [0] 不在 [索引] 中是什么意思?什么是标签 [0] 在我的情况下。
回答by Scotty1-
From the pandas
documentation:
从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)
数据框.loc
通过标签或布尔数组访问一组行和列。.loc[] 主要基于标签,但也可以与布尔数组一起使用。允许的输入是:
单个标签,例如 5 或“a”(请注意,5 被解释为索引的标签,而不是沿索引的整数位置)。
标签列表或数组,例如 ['a', 'b', 'c']。
带有标签的切片对象,例如 'a':'f'。
与被切片的轴长度相同的布尔数组,例如 [True, False, True]。
- 一个可调用函数,带有一个参数(调用 Series、DataFrame 或 Panel)并返回有效的索引输出(上述之一)
Since I guess that ii
is of type integer, you need to use df.iloc
instead:
因为我猜它ii
是整数类型,所以你需要使用df.iloc
:
A = (data.iloc[ii] >= A1) & (data.iloc[ii] < A2)
回答by MatAff
This error also occurs when your index doesn't start with 0. I reset my index using the code below which fixed the error.
当您的索引不是以 0 开头时,也会发生此错误。我使用下面修复了错误的代码重置了我的索引。
train_df = train_df.reset_index()
This does not directly address the issue described in the question, but wanted to leave this here for reference in case anyone comes across the same error in future.
这并没有直接解决问题中描述的问题,但希望将其留在这里以供参考,以防将来有人遇到相同的错误。