Teradata Package for Python Function Reference on VantageCloud Lake | 17.20 - loc - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference on VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Package for Python
Release Number
17.20
Publication Date
November 2022
Content Type
Programming Reference
Publication ID
B700-4500-112K
ft:locale
en-US
ft:lastEdition
2024-02-21
ft:sourceId
FTML
dita:id
TeradataPython_FxRef_Lake_1720
Product Category
Teradata Vantage
teradataml.dataframe.dataframe.DataFrame.loc
Access a group of rows and columns by label(s) or a boolean array.
 
VALID INPUTS:
 
    - A single label, e.g. ``5`` or ``'a'``, (note that ``5`` is
    interpreted as a label of the index, it is not interpreted as an
    integer position along the index).
 
    - A list or array of column or index labels, e.g. ``['a', 'b', 'c']``.
 
    - A slice object with labels, e.g. ``'a':'f'``.
    Note that unlike the usual python slices where the stop index is not included, both the
        start and the stop are included
 
    - A conditional expression for row access.
 
    - A boolean array of the same length as the column axis for column access.
 
RETURNS:
    teradataml DataFrame
 
RAISE:
    TeradataMlException
 
EXAMPLES
--------
    >>> load_example_data("dataframe","sales")
    >>> df = DataFrame('sales')
    >>> df
                Feb   Jan   Mar   Apr    datetime
    accounts
    Blue Inc     90.0    50    95   101  04/01/2017
    Alpha Co    210.0   200   215   250  04/01/2017
    Jones LLC   200.0   150   140   180  04/01/2017
    Yellow Inc   90.0  None  None  None  04/01/2017
    Orange Inc  210.0  None  None   250  04/01/2017
    Red Inc     200.0   150   140  None  04/01/2017
 
    # Retrieve row using a single label.
    >>> df.loc['Blue Inc']
            Feb Jan Mar  Apr    datetime
    accounts
    Blue Inc  90.0  50  95  101  04/01/2017
 
    # List of labels. Note using ``[[]]``
    >>> df.loc[['Blue Inc', 'Jones LLC']]
                Feb  Jan  Mar  Apr    datetime
    accounts
    Blue Inc    90.0   50   95  101  04/01/2017
    Jones LLC  200.0  150  140  180  04/01/2017
 
    # Single label for row and column (index)
    >>> df.loc['Yellow Inc', 'accounts']
    Empty DataFrame
    Columns: []
    Index: [Yellow Inc]
 
    # Single label for row and column
    >>> df.loc['Yellow Inc', 'Feb']
        Feb
    0  90.0
 
    # Single label for row and column access using a tuple
    >>> df.loc[('Yellow Inc', 'Feb')]
        Feb
    0  90.0
 
    # Slice with labels for row and single label for column. As mentioned
    # above, note that both the start and stop of the slice are included.
    >>> df.loc['Jones LLC':'Red Inc', 'accounts']
    Empty DataFrame
    Columns: []
    Index: [Orange Inc, Jones LLC, Red Inc]
 
    # Slice with labels for row and single label for column. As mentioned
    # above, note that both the start and stop of the slice are included.
    >>> df.loc['Jones LLC':'Red Inc', 'Jan']
        Jan
    0  None
    1   150
    2   150
 
    # Slice with labels for row and labels for column. As mentioned
    # above, note that both the start and stop of the slice are included.
    >>> 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
 
    # Empty slice for row and labels for column.
    >>> df.loc[:, :]
                Feb   Jan   Mar    datetime   Apr
    accounts
    Jones LLC   200.0   150   140  04/01/2017   180
    Blue Inc     90.0    50    95  04/01/2017   101
    Yellow Inc   90.0  None  None  04/01/2017  None
    Orange Inc  210.0  None  None  04/01/2017   250
    Alpha Co    210.0   200   215  04/01/2017   250
    Red Inc     200.0   150   140  04/01/2017  None
 
    # Conditional expression
    >>> df.loc[df['Feb'] > 90]
                Feb   Jan   Mar   Apr    datetime
    accounts
    Jones LLC   200.0   150   140   180  04/01/2017
    Red Inc     200.0   150   140  None  04/01/2017
    Alpha Co    210.0   200   215   250  04/01/2017
    Orange Inc  210.0  None  None   250  04/01/2017
 
    # Conditional expression with column labels specified
    >>> df.loc[df['Feb'] > 90, ['accounts', 'Jan']]
                Jan
    accounts
    Jones LLC    150
    Red Inc      150
    Alpha Co     200
    Orange Inc  None
 
    # Conditional expression with multiple column labels specified
    >>> df.loc[df['accounts'] == 'Jones LLC', ['accounts', 'Jan', 'Feb']]
            Jan    Feb
    accounts
    Jones LLC  150  200.0
 
    # Conditional expression and slice with column labels specified
    >>> df.loc[df['accounts'] == 'Jones LLC', 'accounts':'Mar']
            Mar  Jan    Feb
    accounts
    Jones LLC  140  150  200.0
 
    # Conditional expression and boolean array for column access
    >>> df.loc[df['Feb'] > 90, [True, True, False, False, True, True]]
              Feb   Apr    datetime
    accounts
    Alpha Co    210.0   250  04/01/2017
    Jones LLC   200.0   180  04/01/2017
    Red Inc     200.0  None  04/01/2017
    Orange Inc  210.0   250  04/01/2017
    >>>