sort() Method | Teradata Python Package - sort() Method - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Use the sort() method to sort data on one or more columns in either ascending or descending order for a teradataml DataFrame.

The method takes a column name or a list of column names to sort on. Use the ascending parameter to specified ascending or descending order. True for ascending order and False for descending order.

The column used for sorting in sort must have type that supports sorting. Unsupported types include: ‘BLOB’, ‘CLOB’, ‘ARRAY’, ‘VARRAY’.
Required Arguments:
  • columns: Specifies the names of the columns or ColumnExpressions to sort on.
Optional Arguments:
  • ascending: Specifies whether to order in ascending or descending order for each column specified in columns.

    When set to True, sort in ascending order. Otherwise, sort in descending order.

    Default value is True.

    • If a list is specified, length of the ascending must equal length of the columns.
    • If a list is specified, element in ascending is ignored if the corresponding element in columns is a ColumnExpression.
    • This argument is ignored if columns is a ColumnExpression.

Examples Prerequisite

Assume a teradataml DataFrame "df" is created from a Vantage table "admissions_train", using command:
df = DataFrame("admissions_train")

Example 1: Sort data based on the column 'Feb' in ascending order by passing the name of the column

df.sort("Feb")
              Feb    Jan    Mar    Apr    datetime
accounts
Blue Inc     90.0   50.0   95.0  101.0  04/01/2017
Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
Red Inc     200.0  150.0  140.0    NaN  04/01/2017
Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
Alpha Co    210.0  200.0  215.0  250.0  04/01/2017

Example 2: Sort data based on the column 'Feb' in descending order by passing the ColumnExpression

df.sort([df.Feb.desc()])
              Feb    Jan    Mar    Apr    datetime
accounts
Alpha Co    210.0  200.0  215.0  250.0  04/01/2017
Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
Red Inc     200.0  150.0  140.0    NaN  04/01/2017
Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
Blue Inc     90.0   50.0   95.0  101.0  04/01/2017

Example 3: Sort column and ColumnExpression in different ascending but same NULL settings

This example sorts data based on the columns 'Jan' in ascending order and 'accounts' in descending order, respectively, both with NULLS at first.

Since the second element in columns is a ColumnExpression, the data is sorted in descending order even though the second element in ascending is True.
df.sort([df.Jan.nulls_first(), df.accounts.desc().nulls_first()])
              Feb    Jan    Mar    Apr    datetime
accounts
Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
Blue Inc     90.0   50.0   95.0  101.0  04/01/2017
Red Inc     200.0  150.0  140.0    NaN  04/01/2017
Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
Alpha Co    210.0  200.0  215.0  250.0  04/01/2017

Example 4: Sort different columns in different ascending but same NULL settings

This example sorts data based on columns 'Jan' in ascending order with NULLS at first and 'Apr' in descending order with NULLS at first, respectively.

df.sort([df.Jan.nulls_first(), df.Apr.desc().nulls_first()])
              Feb    Jan    Mar    Apr    datetime
accounts
Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
Blue Inc     90.0   50.0   95.0  101.0  04/01/2017
Red Inc     200.0  150.0  140.0    NaN  04/01/2017
Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
Alpha Co    210.0  200.0  215.0  250.0  04/01/2017

Example 5: Sort different columns in different ascending and NULL settings

This example sorts the data based on columns 'Jan' and in ascending order with NULLS at first and 'Apr' in descending order with NULLS at last, respectively.

df.sort([df.Jan.nulls_first(), df.Apr.desc().nulls_last()])
              Feb    Jan    Mar    Apr    datetime
accounts
Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
Blue Inc     90.0   50.0   95.0  101.0  04/01/2017
Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
Red Inc     200.0  150.0  140.0    NaN  04/01/2017
Alpha Co    210.0  200.0  215.0  250.0  04/01/2017