Teradata Package for Python Function Reference on VantageCloud Lake | 17.20 - row_number - 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
17.20
Publication Date
November 2022
Content Type
Programming Reference
Publication ID
B700-4500-112K
ft:locale
en-US
ft:lastEdition
2024-02-21
ft:sourceId
FTML
dita:id
TeradataPython_FxRef_Lake_1720
Product Category
Teradata Vantage
teradataml.dataframe.window.row_number = row_number()
DESCRIPTION:
    Function returns the sequential row number, starting with first row as
    number one, for all the rows in a teradataml DataFrame or ColumnExpression,
    according to "order_columns", over the specified window.
    Notes:
         1. Window parameter "order_columns" should not be None.
         2. Unlike other window aggregate functions, executing row_number()
            on a window created on teradataml DataFrame does not create
            multiple columns, that is, it results in one new column in addition
            to the original teradataml DataFrame columns. For calculating
            row_number, value passed to "order_columns" parameter of window
            is considered and not the ColumnExpression.
 
PARAMETERS:
    None.
 
RETURNS:
    * teradataml DataFrame - When aggregate is executed using window created
      on teradataml DataFrame.
    * ColumnExpression, also known as, teradataml DataFrameColumn - When aggregate is
      executed using window created on ColumnExpression.
 
RAISES:
    RuntimeError - If column does not support the aggregate operation.
 
EXAMPLES:
    # Load the data to run the example.
    >>> load_example_data("dataframe", "admissions_train")
    >>>
 
    # Create a teradataml DataFrame on 'admissions_train' table.
    >>> admissions_train = DataFrame("admissions_train")
    >>> admissions_train
       masters   gpa     stats programming  admitted
    id
    22     yes  3.46    Novice    Beginner         0
    36      no  3.00  Advanced      Novice         0
    15     yes  4.00  Advanced    Advanced         1
    38     yes  2.65  Advanced    Beginner         1
    5       no  3.44    Novice      Novice         0
    17      no  3.83  Advanced    Advanced         1
    34     yes  3.85  Advanced    Beginner         0
    13      no  4.00  Advanced      Novice         1
    26     yes  3.57  Advanced    Advanced         1
    19     yes  1.98  Advanced    Advanced         0
    >>>
 
    # Example 1: Get the row number over the values in a window,
    #            partitioned over 'programming' and sort by 'gpa'.
    # Create a window on 'gpa'.
    >>> window = admissions_train.gpa.window(partition_columns="programming",
    ...                                      order_columns="gpa")
    # Execute row_number() on the window and attach it to the teradataml DataFrame.
    # Note: DataFrame.assign() allows combining multiple window aggregate operations
    #       in one single call. In this example, we are executing row_number() along
    #       with mean() window aggregate operations.
    >>> df = admissions_train.assign(row_number=window.row_number(), mean_gpa=window.mean())
    >>> df
       masters   gpa     stats programming  admitted  mean_gpa  row_number
    id
    32     yes  3.46  Advanced    Beginner         0  3.660000           3
    35      no  3.68    Novice    Beginner         1  3.660000           5
    3       no  3.70    Novice    Beginner         1  3.660000           6
    39     yes  3.75  Advanced    Beginner         0  3.660000           7
    34     yes  3.85  Advanced    Beginner         0  3.660000           9
    21      no  3.87    Novice    Beginner         1  3.660000          10
    19     yes  1.98  Advanced    Advanced         0  3.615625           1
    11      no  3.13  Advanced    Advanced         1  3.615625           2
    14     yes  3.45  Advanced    Advanced         0  3.615625           3
    6      yes  3.50  Beginner    Advanced         1  3.615625           4
    >>>
 
    # Example 2: Get the row number on a window created on
    #            teradataml DataFrame, partitioned over 'masters',
    #            and order by 'gpa'.
    # Create a window on teradataml DataFrame.
    >>> window = admissions_train.window(partition_columns="masters",
    ...                                  order_columns="gpa")
    >>>
    # Execute row_number() on a window.
    >>> df = window.row_number()
    >>> df
       masters   gpa     stats programming  admitted  col_row_number
    id
    38     yes  2.65  Advanced    Beginner         1               3
    32     yes  3.46  Advanced    Beginner         0               5
    22     yes  3.46    Novice    Beginner         0               6
    4      yes  3.50  Beginner      Novice         1               7
    6      yes  3.50  Beginner    Advanced         1               9
    26     yes  3.57  Advanced    Advanced         1              10
    24      no  1.87  Advanced      Novice         1               1
    36      no  3.00  Advanced      Novice         0               2
    11      no  3.13  Advanced    Advanced         1               3
    5       no  3.44    Novice      Novice         0               4
    >>>