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

2024-05-22 20:29:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

myfun(B,10)

它在函数的这一行给出了错误

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

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

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

我看了你网站上的所有内容,这不适用于我的案件。因为没有人解释过这个错误是什么意思。

有人能告诉我哪里可能有问题,我怎么解决吗? 什么意思说标签[0]不在[index]中?什么是标签[0]在我的情况下。


Tags: the数据函数代码a2dataindexa1
2条回答

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)

当索引不是以0开头时,也会发生此错误。我使用下面修复错误的代码重置索引。

train_df = train_df.reset_index()

这并不能直接解决问题中描述的问题,但希望将其留在这里作为参考,以防将来有人遇到相同的错误。

相关问题 更多 >

    热门问题