pandas.Series.iloc

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

See more at Selection by Position.

Limitations

Iloc always returns Series.

Examples

With a scalar integer.
import numpy as np
import pandas as pd
from numba import njit


@njit
def series_iloc_value():
    series = pd.Series([5, 4, 3, 2, 1], index=[0, 2, 4, 6, 8])

    return series.iloc[4]  # Expect value: 1


print(series_iloc_value())
$ python ./series/series_iloc/series_iloc_value.py
1
With a slice object.
import numpy as np
import pandas as pd
from numba import njit


@njit
def series_iloc_slice():
    series = pd.Series([5, 4, 3, 2, 1], index=[0, 2, 4, 6, 8])

    return series.iloc[2:4]


print(series_iloc_slice())
$ python ./series/series_iloc/series_iloc_slice.py
4    3
6    2
dtype: int64

See also

DataFrame.iat

Fast integer location scalar accessor.

DataFrame.loc

Purely label-location based indexer for selection by label.

Series.iloc

Purely integer-location based indexing for selection by position.