Teradata Package for Python Function Reference on VantageCloud Lake - sort - 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
- 20.00.00.03
- Published
- December 2024
- ft:locale
- en-US
- ft:lastEdition
- 2024-12-19
- dita:id
- TeradataPython_FxRef_Lake_2000
- Product Category
- Teradata Vantage
- teradataml.dataframe.dataframe.DataFrame.sort = sort(self, columns, ascending=True)
- DESCRIPTION:
Get sorted data by one or more columns in either ascending or descending order
for a Dataframe.
Unsupported column types for sorting: ['BLOB', 'CLOB', 'ARRAY', 'VARRAY']
PARAMETERS:
columns:
Required Argument.
Specifies the name(s) of the columns or ColumnExpression(s) to sort on.
Types: str OR ColumnExpression OR list of Strings (str) OR list of ColumnExpressions.
ascending:
Optional Argument.
Specifies whether to order in ascending or descending order for each column.
When set to True, sort in ascending order. Otherwise, sort in descending order.
Notes:
* If a list is specified, length of the 'ascending' must equal
length of the "columns".
* If a list is specified, element is ignored if the corresponding
element in "columns" is a ColumnExpression.
* The argument is ignored if "columns" is a ColumnExpression.
Default value: True
Types: bool or list of bool
RETURNS:
teradataml DataFrame
RAISES:
TeradataMlException
EXAMPLES:
>>> load_example_data("dataframe", "sales")
>>> df = DataFrame("sales")
>>> df
Feb Jan Mar Apr datetime
accounts
Blue Inc 90.0 50.0 95.0 101.0 04/01/2017
Alpha Co 210.0 200.0 215.0 250.0 04/01/2017
Jones LLC 200.0 150.0 140.0 180.0 04/01/2017
Yellow Inc 90.0 NaN NaN NaN 04/01/2017
Orange Inc 210.0 NaN NaN 250.0 04/01/2017
Red Inc 200.0 150.0 140.0 NaN 04/01/2017
# Example 1: Sort the 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 the 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 the data based on the columns Jan and accounts in
# ascending order and descending order with NULLS at first
# respectively.
# Note:
# * 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(["Jan", df.accounts.desc().nulls_first()], [True, True])
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 the data based on columns Jan and Apr in ascending order
# with NULLS at first and 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 the data based on columns Jan and Apr in ascending order
# with NULLS at first and 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
>>>