69

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.

Why should I ever use .loc() and .iloc() if .ix() is faster and supports integer and label access?

zx8754
  • 52,746
  • 12
  • 114
  • 209
megashigger
  • 8,695
  • 17
  • 47
  • 79
  • 1
    This answer [here](http://stackoverflow.com/a/31593712/2303761) should be VERY helpful! – YaOzI Nov 15 '16 at 06:47
  • 2
    Never use **`.ix`**! [Here is an even more helpful answer](https://stackoverflow.com/a/46915810/3707607) for the differences between `.loc`, `.iloc` and `[]` – Ted Petrou Nov 04 '17 at 04:13
  • Possible duplicate of [pandas iloc vs ix vs loc explanation?](https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation) – Yolo Voe Jan 30 '18 at 19:27

2 Answers2

71

Please refer to the doc Different Choices for Indexing, it states clearly when and why you should use .loc, .iloc over .ix, it's about explicit use case:

.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.

Update 22 Mar 2017

Thanks to comment from @Alexander, Pandas is going to deprecate ix in 0.20, details in here.

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.

It is expected to migrate to use iloc and loc instead, here is a link on how to convert code.

Anzel
  • 19,825
  • 5
  • 51
  • 52
  • 1
    can you clarify what the "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." means? That part is hazy for me – AZhao Jul 23 '15 at 13:50
  • 1
    @AZhao, I don't think I understand what you want me to clarify, do you want to know the differences between chioces of index or how to determine "explicit"? Perhaps you may read [Difference choices of indexing](http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing). – Anzel Jul 24 '15 at 09:36
  • 3
    @AZhao, I think this means that if you use integers *as* your labels (which might not correspond to their column order), then it won't be able to differentiate which one you mean. Someone correct me if I'm misinterpreting. – Neal Nov 19 '16 at 19:58
  • 3
    It appears that the `.ix` indexer will be deprecated in Pandas 0.20.0 http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#deprecate-ix – Alexander Mar 22 '17 at 01:04
  • @Alexander - thanks for the deprecation note, I will update the answer to reflect this :) – Anzel Mar 22 '17 at 09:15
0

DataFrame.ix has been completely removed since Pandas 1.0.0 released in January 2020, see https://pandas.pydata.org/pandas-docs/stable/whatsnew/v1.0.0.html#removal-of-prior-version-deprecations-changes