pandas 熊猫:使用 ix 索引越界,但我可以看到该列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36071702/
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
pandas: index out of bounds using ix, but I can see the column
提问by camdenl
I have no idea why pandas is throwing an index out of bounds on this dataframe:
我不知道为什么Pandas会在这个数据帧上抛出一个越界的索引:
SC7a 2009 2010 2011 2012 2013 2014
Region 10 10.1 10.6 11.1 11.6 9.7 10.8
Georgia 7.5 7.4 7.8 7.6 7.2 7.1
All I am doing is calling:
我正在做的就是打电话:
df.ix[:, 2014]
And I get this error:
我收到这个错误:
IndexError: index 2014 is out of bounds for axis 0 with size 6
Note that calling loc works just fine:
请注意,调用 loc 工作正常:
df.loc[:, 2014]
SC7a
Region 10 10.8
Georgia 7.1
Name: 2014, dtype: float64
Is this a bug? Shouldn't df.loc and df.ix be interchangeable with this data?
这是一个错误吗?df.loc 和 df.ix 不应该与这些数据互换吗?
回答by Alex Riley
I'm guessing that your df.columns.dtype
is of the objectdata type and also that testing df.columns.is_integer()
returns false.
我猜你df.columns.dtype
是对象数据类型,并且测试df.columns.is_integer()
返回false。
The behaviour of ix
is dependent on the data type or contents of the index. If you have an integertype index or .is_integer()
is true, ix
uses label-based indexing (like loc
). If you have another type of index (e.g. float, object), then ix
uses position-based indexing when given an integer to find (like iloc
). In this latter case, index position 2014 is out of bounds.
的行为ix
取决于索引的数据类型或内容。如果您有整数类型索引或.is_integer()
为真,则ix
使用基于标签的索引(如loc
)。如果您有另一种类型的索引(例如float、object),则ix
在给定整数时使用基于位置的索引进行查找(例如iloc
)。在后一种情况下,索引位置 2014 超出范围。
If you convert your columns to integer type, ix
will work as you expect it to (it will only use label-based indexing):
如果您将列转换为整数类型,ix
将按您的预期工作(它将仅使用基于标签的索引):
>>> df.columns = df.columns.astype(int)
>>> df.ix[:, 2014]
Region 10 10.8
Georgia 7.1
Name: 2014, dtype: float64
回答by Alexander
It is because all of your columns are integers. Per the docs:
这是因为您的所有列都是整数。根据文档:
.ix supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type. .ix is the most general and will support any of the inputs in .loc and .iloc. .ix also supports floating point label schemes. .ix is exceptionally useful when dealing with mixed positional and label based hierarchical indexes.
However, when an axis is integer based, ONLY label based access and not positional access is supported. Thus, in such cases, it's usually better to be explicit and use .iloc or .loc.
.ix 支持混合整数和基于标签的访问。它主要基于标签,但除非相应的轴是整数类型,否则将回退到整数位置访问。.ix 是最通用的,将支持 .loc 和 .iloc 中的任何输入。.ix 还支持浮点标签方案。.ix 在处理混合位置和基于标签的分层索引时非常有用。
但是,当轴基于整数时,仅支持基于标签的访问而不支持位置访问。因此,在这种情况下,通常最好明确并使用 .iloc 或 .loc。