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
andiloc
- to make the explicit distinction between positional and label based indexing. Please beware thatix
was discontinued due to inconsistent behavior and being hard to grok, and no longer exists in current versions of pandas (>= 1.0).
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.
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.
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.
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.
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
andiat
are meant to access a scalar, that is, a single element in the dataframe, whileloc
andiloc
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With