index[] 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 index operator ([ ]) to filter rows based on logical expressions given by Python literals and DataFrame columns.

Supported Types and Operators

Python int, float, Decimal, str, and None literals can be used in filtering expressions.

All arithmetic expressions except floor division (//) and power (**) can be used.

All logical operators are supported.

Examples Prerequisite

Assume the DataFrame module is imported using the command:

from teradataml import DataFrame

And assume a teradataml DataFrame "df" is created from a Teradata table "iris", using command:

df = DataFrame('iris').select(['SepalLength', 'SepalWidth'])
length = df.SepalLength
width = df.SepalWidth

Example: Inequality operations

The operators supported are <, <=, >, >=.

df[length > width]
 
 
   SepalLength  SepalWidth
0          5.1         3.5
1          7.7         2.8
2          5.1         3.8
3          5.9         3.0
4          6.0         2.9
5          5.0         3.5
6          5.5         2.4
7          6.9         3.2
8          4.4         3.0
9          5.1         3.5
df[length < width]
 
 
   SepalLength  SepalWidth
0        1.012       1.202

Example: Equality operations

The operators supported are == and !=.

df[length == width]
 
Empty DataFrame
Columns: [SepalLength, SepalWidth]
Index: []
df[length != width]
 
 
   SepalLength  SepalWidth
0          6.0         2.9
1          5.5         2.4
2          6.9         3.2
3          5.8         2.7
4          5.1         3.5
5          5.1         3.5
6          7.7         2.8
7          5.1         3.8
8          6.9         3.1
9          5.0         3.5

Example: Boolean operations

The operators supported are &, |, ~.

df[~(length == width)]
 
 
   SepalLength  SepalWidth
0          6.0         2.9
1          7.7         2.8
2          5.1         3.8
3          5.0         3.5
4          6.9         3.2
5          5.8         2.7
6          6.9         3.1
7          5.1         3.5
8          5.5         2.4
9          5.1         3.5
df[(length > 3) & (width < 3)]
 
 
   SepalLength  SepalWidth
0          5.0         2.3
1          5.2         2.7
2          5.7         2.8
3          5.5         2.4
4          7.7         2.6
5          5.8         2.7
6          5.5         2.5
7          6.2         2.2
8          4.9         2.4
9          7.7         2.8
df[(2 >= length) | (4 <= width)]
 
 
   SepalLength  SepalWidth
0        5.200       4.100
1        5.500       4.200
2        1.012       1.202
3        5.800       4.000
4        5.700       4.400

It is recommended to surround expressions in parentheses in order to avoid ambiguity in operator precedence, similar to pandas.

For example, if in the example above, the expression is taken without the parenthesis, an error is given.
# error
 
df[2 >= length | 4 <= width]
 
 
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
...
ArgumentError: SQL expression object or string expected, got object of type <class 'int'> instead
...
TeradataMlException: [Teradata][teradataml](TDML_2021) Unable to retrieve information for the DataFrame.