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.
Unsupported types include: 'BLOB', 'CLOB', 'ARRAY', 'VARRAY'.
- columns: Specifies the names of the columns or ColumnExpressions to sort on.
- 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
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.
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