.iloc[] is the Pandas function for accessing by integer-location:

Purely integer-location based indexing for selection by position.

.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

Allowed inputs are:

- An integer, e.g. 5.
- A list or array of integers, e.g. [4, 3, 0].
- A slice object with ints, e.g. 1:7.
- A boolean array.
- A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above). This is useful in method chains, when you don’t have a reference to the calling object, but would like to base your selection on some value.

.iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing (this conforms with python/numpy slice semantics).


I don't remember why df.iloc[3] is preferred over directly getitem'ing df[3] ?

- Because `3` could be a key or an integer-location; to avoid ambiguity
- Because changing between df[3] and df.loc[3] and df[3:5] and df.iloc[3:5] is unnecessary cognitive burden: it's easier to determine that the code is intentionally accessing by integer-location from '.iloc' than from ':' (indicating slice notation)

>>> odict.iloc[3]

Would this be the only way to access only item 4 from an odict of length greater than 4 with slice notation for dict views generated from selection by integer-location?

>>> odict[3:4]

What does this do? Confusing to a beginner:

>>> odict[3,]

On Wed, Jul 29, 2020, 1:28 AM Stephen J. Turnbull <turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
Christopher Barker writes:

 > from itertools import islice
 > smaller_dict = dict(islice(large_dict.items(), 0, 255))
 > which works, and isn't doing an unnecessary copying but it's pretty
 > darn ugly, as far as I'm concerned.

In your application, I think that's just pretty, myself.  The only thing
that's missing is slice notation.  But that's probably not hard to
implement in terms of the islice function, just completely redundant.
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/LPHMWNCI3VPPN7FEBEUJR4K2QDVNCPX3/
Code of Conduct: http://python.org/psf/codeofconduct/