loc[] Operator - Teradata Package for Python

Teradata® Package for Python User Guide

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2022-01-14
dita:mapPath
bol1585763678431.ditamap
dita:ditavalPath
ayr1485454803741.ditaval
dita:id
B700-4006
lifecycle
previous
Product Category
Teradata Vantage

Use the loc[] operator to access a group of rows and columns by labels.

The operator takes a single label, a list of column or index labels, and a slice with labels as valid inputs. It also takes a conditional expression for row access and a list of booleans for column access. The list must include a boolean value for each column.

Examples Prerequisite

Assume a teradataml DataFrame "df" is created from a Vantage table "sales", using command:

>>> df = DataFrame('sales')

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

Example 1: Retrieve a row using a single index label

This example retrieves a row using a single index label "Blue Inc":

>>> df.loc['Blue Inc']
           Feb Jan Mar  Apr    datetime
accounts
Blue Inc  90.0  50  95  101  2017-04-01

Example 2: Retrieve multiple rows using a list of labels

This example uses a list of labels to retrieve the rows for "Blue Inc" and "Jones LLC":

>>> df.loc[['Blue Inc', 'Jones LLC']]
             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

Example 3: Retrieve using a single index label and a single index column label

This example uses a single index label and a single column label (index column label) for row and column access:

>>> df.loc['Yellow Inc', 'accounts']
Empty DataFrame
Columns: []
Index: [Yellow Inc]

Example 4: Retrieve using a single index label and a single non-index column label

This example uses a single index label "Yellow Inc" and a single column label "Feb" (non-index column label) for row and column access:

>>> df.loc['Yellow Inc', 'Feb']
    Feb
0  90.0

Example 5: Retrieve using a slice with labels for row access and single label for column access

This example uses a slice with labels for row access and single label for column access:

>>> df.loc['Jones LLC':'Red Inc', 'accounts']
Empty DataFrame
Columns: []
Index: [Orange Inc, Jones LLC, Red Inc]
Both the start and stop of the slice are included.

Example 6: Retrieve using a slice with labels for row access and for column access

This example uses a slice with labels for row access and for column access:

>>> df.loc['Jones LLC':'Red Inc', 'accounts':'Apr']
             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 7: Retrieve using an empty slice for row access and for column access

This example uses an empty slice for row access and for column access:

>>> df.loc[:, :]
              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 8: Retrieve rows using a conditional expression

This example uses a conditional expression to retrieve rows where the value for "Feb" is greater than 90:

>>> df.loc[df['Feb'] > 90]
              Feb   Jan   Mar   Apr    datetime
accounts
Jones LLC   200.0   150   140   180  2017-04-01
Red Inc     200.0   150   140  None  2017-04-01
Alpha Co    210.0   200   215   250  2017-04-01
Orange Inc  210.0  None  None   250  2017-04-01

Example 9: Retrieve using a conditional expression for row access with multiple column labels for column access

This examples uses a conditional expression for row access with multiple column labels for column access:

>>> df.loc[df['accounts'] == 'Jones LLC', ['accounts', 'Jan', 'Feb']]
           Jan    Feb
accounts
Jones LLC  150  200.0

Example 10: Retrieve using a conditional expression for row access and boolean array for column access

This example uses a conditional expression for row access and boolean array for column access:

>>> df.loc[df['Feb'] > 90, [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