Teradata Package for Python Function Reference - 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

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2021-11-19
lifecycle
previous
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.
        Column names as a string or a list of strings to sort on.
        Types: str OR list of Strings (str)
 
    ascending:
        Optional Argument.
        Order ASC or DESC to be applied for each column.
        True for ascending order and False for descending order.
        Default value: True
        Types: bool
 
RETURNS:
    teradataml DataFrame
 
RAISES:
    TeradataMlException
 
EXAMPLES:
    >>> load_example_data("dataframe","admissions_train")
    >>> df = DataFrame('admissions_train')
    >>> df
       masters   gpa     stats programming admitted
    id
    22     yes  3.46    Novice    Beginner        0
    37      no  3.52    Novice      Novice        1
    35      no  3.68    Novice    Beginner        1
    12      no  3.65    Novice      Novice        1
    4      yes  3.50  Beginner      Novice        1
    38     yes  2.65  Advanced    Beginner        1
    27     yes  3.96  Advanced    Advanced        0
    39     yes  3.75  Advanced    Beginner        0
    7      yes  2.33    Novice      Novice        1
    40     yes  3.95    Novice    Beginner        0
    >>> df.sort("id")
       masters   gpa     stats programming admitted
    id
    1      yes  3.95  Beginner    Beginner        0
    2      yes  3.76  Beginner    Beginner        0
    3       no  3.70    Novice    Beginner        1
    4      yes  3.50  Beginner      Novice        1
    5       no  3.44    Novice      Novice        0
    6      yes  3.50  Beginner    Advanced        1
    7      yes  2.33    Novice      Novice        1
    8       no  3.60  Beginner    Advanced        1
    9       no  3.82  Advanced    Advanced        1
    10      no  3.71  Advanced    Advanced        1
    >>> df.sort(["id"])
       masters   gpa     stats programming admitted
    id
    1      yes  3.95  Beginner    Beginner        0
    2      yes  3.76  Beginner    Beginner        0
    3       no  3.70    Novice    Beginner        1
    4      yes  3.50  Beginner      Novice        1
    5       no  3.44    Novice      Novice        0
    6      yes  3.50  Beginner    Advanced        1
    7      yes  2.33    Novice      Novice        1
    8       no  3.60  Beginner    Advanced        1
    9       no  3.82  Advanced    Advanced        1
    10      no  3.71  Advanced    Advanced        1
    >>> df.sort(["masters","gpa"])
       masters   gpa     stats programming admitted
    id
    24      no  1.87  Advanced      Novice        1
    36      no  3.00  Advanced      Novice        0
    11      no  3.13  Advanced    Advanced        1
    5       no  3.44    Novice      Novice        0
    37      no  3.52    Novice      Novice        1
    33      no  3.55    Novice      Novice        1
    8       no  3.60  Beginner    Advanced        1
    12      no  3.65    Novice      Novice        1
    35      no  3.68    Novice    Beginner        1
    16      no  3.70  Advanced    Advanced        1
    >>> # In next example, sort dataframe with masters column in Ascending ('True')
    >>> # order and gpa column with Descending (False)
    >>> df.sort(["masters","gpa"], ascending=[True,False])
       masters   gpa     stats programming admitted
    id
    13      no  4.00  Advanced      Novice        1
    25      no  3.96  Advanced    Advanced        1
    28      no  3.93  Advanced    Advanced        1
    21      no  3.87    Novice    Beginner        1
    17      no  3.83  Advanced    Advanced        1
    9       no  3.82  Advanced    Advanced        1
    10      no  3.71  Advanced    Advanced        1
    3       no  3.70    Novice    Beginner        1
    16      no  3.70  Advanced    Advanced        1
    35      no  3.68    Novice    Beginner        1
    >>>