...

/

Indexing Operations (.iloc)

Indexing Operations (.iloc)

Learn how to retrieve and manipulate records based on a positional index.

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:

Press + to interact
print(city2.iloc[0])

We can also use negative indexing to pull out the last value:

Press + to interact
print(city2.iloc[-1])

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 ...