Python .ix() 总是比 .loc() 和 .iloc() 更好,因为它更快并且支持整数和标签访问?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27667759/
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
Is .ix() always better than .loc() and .iloc() since it is faster and supports integer and label access?
提问by megashigger
I'm learning the Python pandas library. Coming from an R background, the indexing and selecting functions seem more complicated than they need to be. My understanding it that .loc() is only label based and .iloc() is only integer based.
我正在学习 Python 熊猫库。来自 R 背景,索引和选择功能似乎比它们需要的更复杂。我的理解是 .loc() 仅基于标签,而 .iloc() 仅基于整数。
Why should I ever use .loc() and .iloc() if .ix() is faster and supports integer and label access?
如果 .ix() 更快并且支持整数和标签访问,我为什么要使用 .loc() 和 .iloc() ?
采纳答案by Anzel
Please refer to the doc Different Choices for Indexing, it states clearly when and why you should use .loc, .ilocover .ix, it's about explicit use case:
请参阅 doc Different Choices for Indexing,它清楚地说明了何时以及为什么应该使用.loc, .ilocover .ix,这是关于显式用例的:
.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 hierachical 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。
Hope this helps.
希望这可以帮助。
Update 22 Mar 2017
2017 年 3 月 22 日更新
Thanks to comment from @Alexander, Pandasis going to deprecate ix
in 0.20, details in here.
感谢@Alexander 的评论,Pandas将ix
在0.20 中弃用,详情请见此处。
One of the strong reason behind is because mixing indexes -- positional and label (effectively using ix
) has been a significant source of problems for users.
背后的一个强有力的原因是因为混合索引——位置和标签(有效地使用ix
)一直是用户问题的重要来源。
It is expected to migrate to use iloc
and loc
instead, here is a link on how to convert code.
预计会迁移到使用iloc
,loc
相反,这里有一个关于如何转换代码的链接。