Indexing Operations (.iloc)
Explore how to effectively use the iloc attribute in pandas for position-based indexing and slicing of Series data. Understand different input types like scalars, lists, slices, boolean arrays, and functions. Learn to use head, tail, and sampling methods for quick data inspection, and apply filter and reindex techniques for flexible data selection and alignment.
We'll cover the following...
The iloc attribute
The series also supports indexing off the iloc attribute. This attribute is analogous to loc but with a few differences. We pull out items by index position when we slice off this attribute. The iloc attribute supports indexing with the following:
- A scalar index position (an integer).
- A list of index positions.
- A slice of positions (half-open interval so it doesn’t include stop value).
- A NumPy array (or Python list) of boolean values.
- A function that accepts a Series and returns one of the above.
In the examples below, we’ll pull out the first and last values by slicing off of iloc with a scalar. Note that because index positions are unique, we’ll always get the scalar value when indexing with iloc at a position:
We can also use negative indexing to pull out the last value:
Indexing off the iloc attribute will return a scalar by location in the Series.
If we want to return a Series object, we can index it with a list of positions. This can be a list with a single index in it or ...