iloc[] Operator

Teradata® Python Package User Guide

brand
Teradata Vantage
prodname
Teradata Python Package
vrm_release
16.20
category
User Guide
featnum
B700-4006-098K

Use the iloc[] operator to access a group of rows and columns by integer values.

The operator takes a single integer, a list of integers, and a slice with integers as valid inputs. It also takes a list of booleans for column access. The list must include a boolean value for each column.

For integer indexing on row access, the integer index values are applied to a sorted teradataml DataFrame on the index column or the first column if there is no index column.

Examples Prerequisite

Assume a teradataml DataFrame "df" is created from a Teradata table "sales" and sorted using commands:

>>> df = DataFrame('sales')
>>> df.sort("accounts")
              Feb   Jan   Mar   Apr    datetime
accounts
Alpha Co    210.0   200   215   250  2017-04-01
Blue Inc     90.0    50    95   101  2017-04-01
Jones LLC   200.0   150   140   180  2017-04-01
Orange Inc  210.0  None  None   250  2017-04-01
Red Inc     200.0   150   140  None  2018-10-15
Yellow Inc   90.0  None  None  None  2017-04-01

Example: Retrieve a row using a single integer value

The following example retrieves a row using a single integer value.
>>> df.iloc[1]
        Feb Jan Mar  Apr    datetime
accounts
Blue Inc  90.0  50  95  101  2017-04-01

Example: Retrieve using a list of integers

Using "[[]]" to include a list of integers.
>>> df.iloc[[1, 2]]
            Feb  Jan  Mar  Apr    datetime
accounts
Blue Inc    90.0   50   95  101  2017-04-01
Jones LLC  200.0  150  140  180  2017-04-01

Examples: Use a single integer for both row and column

>>> df.iloc[5, 0]
Empty DataFrame
Columns: []
Index: [Yellow Inc]
>>> df.iloc[(5, 1)]
    Feb
0  90.0

Example: Use a slice for row and a single integer for column access

The stop for the slice is excluded.
>>> df.iloc[2:5, 2]
    Jan
0  None
1   150
2   150

Example: Use a slice for row and column access

The stop for the slice is excluded.
>>> df.iloc[2:5, 0:5]
            Mar   Jan    Feb   Apr
accounts
Orange Inc  None  None  210.0   250
Red Inc      140   150  200.0  None
Jones LLC    140   150  200.0   180

Example: Use an empty slice for row and column access

>>> df.iloc[:, :]
            Feb   Jan   Mar    datetime   Apr
accounts
Jones LLC   200.0   150   140  2017-04-01   180
Blue Inc     90.0    50    95  2017-04-01   101
Yellow Inc   90.0  None  None  2017-04-01  None
Orange Inc  210.0  None  None  2017-04-01   250
Alpha Co    210.0   200   215  2017-04-01   250
Red Inc     200.0   150   140  2017-04-01  None

Example: Use a list of integers and boolean array for column access

>>> df.iloc[[0, 2, 3, 4], [True, True, False, False, True, True]]
            datetime   Apr    Feb
accounts
Jones LLC   2017-04-01   180  200.0
Orange Inc  2017-04-01   250  210.0
Alpha Co    2017-04-01   250  210.0
Red Inc     2017-04-01  None  200.0