Python熊猫:.iloc超出了位置索引器的范围

时间:2020-03-26 04:58:04

标签: python pandas indexing

df.tail()返回:

35114   False   False   0.0 False   False   0.0 False   False   0.0 0.0 0.0 False   
35115   False   False   0.0 False   False   0.0 False   False   0.0 0.0 0.0 False   
35116   False   False   0.0 False   False   0.0 False   False   0.0 0.0 0.0 False   
35117   False   False   0.0 False   False   0.0 False   False   0.0 0.0 0.0 False   
35118   False   False   0.0 False   False   0.0 False   False   0.0 0.0 0.0 False

df.iloc [35118]返回:

IndexError: single positional indexer is out-of-bounds

df.iloc [[35118]]返回:

IndexError: positional indexers are out-of-bounds

df.iloc [[10]]返回:

10   False   False   0.0   False   ....

不要介意我可以做的.astype(int),为什么不能通过明显存在的索引来建立该索引?

df.index [-10:]返回:

Int64Index([35109, 35110, 35111, 35112, 35113, 35114, 35115, 35116, 35117,
            35118], dtype='int64')

1 个答案:

答案 0 :(得分:0)

如果尝试使用 iloc [n] 访问行,则数字( n )应该为 少于行数。在这种情况下,您尝试访问 从 0 开始通过其位置(不是索引值)的行。

因此,从运行df.index.size开始,显示的数字将显示如何 你有很多行。

显然,您的DataFrame中的行号小于或等于 您传递的数字(缺少一些索引值)。

另一方面,您也可以使用 loc [n] 访问行,这一次 索引

例如df.loc[35118]将返回索引等于给定值的行, 就您而言,最后一行。