Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas loc vs. iloc vs. at vs. iat?

Recently began branching out from my safe place (R) into Python and and am a bit confused by the cell localization/selection in Pandas. I've read the documentation but I'm struggling to understand the practical implications of the various localization/selection options.

Is there a reason why I should ever use .loc or .iloc over at, and iat or vice versa? In what situations should I use which method?


Note: future readers be aware that this question is old and was written before pandas v0.20 when there used to exist a function called .ix. This method was later split into two - loc and iloc - to make the explicit distinction between positional and label based indexing. Please beware that ix was discontinued due to inconsistent behavior and being hard to grok, and no longer exists in current versions of pandas (>= 1.0).

like image 615
scribbles Avatar asked Feb 27 '15 04:02

scribbles


People also ask

What is the difference between ILOC and IAT?

What is the difference between IAT and ILOC? Similarly to loc, at provides label based scalar lookups, while, iat provides integer based lookups analogously to iloc. Show activity on this post. iat and at gives only a single value output, while iloc and loc can give multiple row output.

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.

What is IAT in pandas?

iat[source] Access a single value for a row/column pair by integer position. Similar to iloc , in that both provide integer-based lookups. Use iat if you only need to get or set a single value in a DataFrame or Series.

Should I use loc or ILOC?

loc is used to index a pandas DataFrame or Series using labels. On the other hand, iloc can be used to retrieve records based on their positional index.


1 Answers

loc: only work on index
iloc: work on position
at: get scalar values. It's a very fast loc
iat: Get scalar values. It's a very fast iloc

Also,

at and iat are meant to access a scalar, that is, a single element in the dataframe, while loc and iloc are ments to access several elements at the same time, potentially to perform vectorized operations.

http://pyciencia.blogspot.com/2015/05/obtener-y-filtrar-datos-de-un-dataframe.html

like image 64
lautremont Avatar answered Sep 16 '22 18:09

lautremont