Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is .ix() always better than .loc() and .iloc() since it is faster and supports integer and label access?

Tags:

python

pandas

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?

like image 483
megashigger Avatar asked Dec 27 '14 13:12

megashigger


People also ask

Is IX deprecated in Pandas?

ix is deprecated from Pandas version 0.20. 0. You can use the more strict indexing method like loc and iloc .

Is ILOC slower than LOC?

loc . I have a DataFrame with 4.8 million rows, and selecting a single row using . iloc[[ id ]] (with a single-element list) takes 489 ms, almost half a second, 1,800x times slower than the identical .

What is the difference between LOC () and ILOC ()?

The main distinction between the two methods is: loc gets rows (and/or columns) with particular labels. iloc gets rows (and/or columns) at integer locations.

Is IX same as ILOC?

iloc — gets rows (or columns) at particular positions in the index (so it only takes integers). ix — usually behaves like loc but falls back to behaving like iloc if a label is not present in the index.


1 Answers

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.

like image 56
Anzel Avatar answered Sep 23 '22 01:09

Anzel