sort() Method | Teradata Python Package - 17.00 - sort() Method - Teradata Package for Python

Teradata® Package for Python User Guide

Product
Teradata Package for Python
Release Number
17.00
Release Date
November 2021
Content Type
User Guide
Publication ID
B700-4006-070K
Language
English (United States)

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’.

Examples Prerequisite

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

Example 1: Sort in ascending order

This example sorts in ascending order on the column "id" of "admissions_train":

>>> 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

Example 2: Sort in descending order

This example sorts in descending order on the column "id" of "admissions_train":

>>> df.sort("id", ascending=False)
   masters   gpa     stats programming admitted
id
40     yes  3.95    novice    beginner        0
39     yes  3.75  advanced    beginner        0
38     yes  2.65  advanced    beginner        1
37      no  3.52    novice      novice        1
36      no  3.00  advanced      novice        0
35      no  3.68    novice    beginner        1
34     yes  3.85  advanced    beginner        0
33      no  3.55    novice      novice        1
32     yes  3.46  advanced    beginner        0
31     yes  3.50  advanced    beginner        1

Example 3: Sort in ascending order on multiple columns

This example sorts in ascending order on the columns "masters" and "gpa":

>>> display.max_row = 20

>>> 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
3       no  3.70    novice    beginner        1
16      no  3.70  advanced    advanced        1
10      no  3.71  advanced    advanced        1
9       no  3.82  advanced    advanced        1
17      no  3.83  advanced    advanced        1
21      no  3.87    novice    beginner        1
28      no  3.93  advanced    advanced        1
25      no  3.96  advanced    advanced        1
13      no  4.00  advanced      novice        1
19     yes  1.98  advanced    advanced        0
7      yes  2.33    novice      novice        1