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

Deployment
VantageCloud
VantageCore
Edition
VMware
Enterprise
IntelliFlex
Product
Teradata Package for Python
Release Number
20.00.00.11
Published
August 2026
ft:locale
en-US
ft:lastEdition
2026-08-13
dita:id
TeradataPython_FxRef_Enterprise_2000
Product Category
Teradata Vantage
teradataml.dataframe.sql.DataFrameColumn.array_agg = array_agg(self, array_type=None, value_expr=None, elements_order=None)
DESCRIPTION:
    Aggregate the rows of a column into an array.
 
    Notes:
        * One should always use "drop_columns=True" in DataFrame.assign(), while
          running the aggregate operation on teradataml DataFrame.
        * "drop_columns" argument in DataFrame.assign() is ignored, when aggregate
          function is operated on DataFrame.groupby().
 
PARAMETERS:
    array_type:
        Optional Argument.
        Specifies the type of the array to which the rows should be aggregated.
        Note:
            * If not specified, teradataml derives the type from the column type.
        Types: teradatasqlalchemy.types
 
    value_expr:
        Optional Argument.
        Specifies the column used to order the elements during aggregation.
        Types: str OR ColumnExpression
 
    elements_order:
        Optional Argument.
        Specifies the order in which the elements are aggregated.
        Permitted Values: ASC, DESC
        Types: str
 
RETURNS:
    ColumnExpression.
 
RAISES:
    TypeError, ValueError, TeradataMlException
 
EXAMPLES:
    # Load the data to run the example.
    >>> load_example_data("dataframe", "admissions_train")
    
    # Create a DataFrame on 'admissions_train' table.
    >>> df = DataFrame("admissions_train")
    >>> df
        masters   gpa     stats programming  admitted
    id                                              
    34     yes  3.85  Advanced    Beginner         0
    32     yes  3.46  Advanced    Beginner         0
    11      no  3.13  Advanced    Advanced         1
    40     yes  3.95    Novice    Beginner         0
    38     yes  2.65  Advanced    Beginner         1
    36      no  3.00  Advanced      Novice         0
    7      yes  2.33    Novice      Novice         1
    26     yes  3.57  Advanced    Advanced         1
    19     yes  1.98  Advanced    Advanced         0
    13      no  4.00  Advanced      Novice         1
    >>>
 
    # Example 1: Aggregate the values in 'admitted' column into an array column.
    >>> res = df.assign(True, admitted_array=df.admitted.array_agg())
    >>> res
                                                                          admitted_array
    0  (0,0,1,0,1,1,0,1,1,0,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,0,1,1,1,1,0,0)
 
    # Example 2: Aggregate values in 'admitted' column into a bigint type array column 
    #            and ordering them using 'id' column in descending order.
    >>> from teradatasqlalchemy.types import ARRAY_BIGINT
    >>> res = df.assign(True, admitted_array=df.admitted.array_agg(ARRAY_BIGINT('[100]'), df.id, 'DESC'))
    >>> res
                                                                          admitted_array
    0  (0,0,1,1,0,1,0,1,0,1,0,0,1,0,1,1,1,1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,0)
 
    # Example 3: Get the array of values in 'id' column for each programming level.
    >>> res = df.groupby("programming").assign(id_array=df.id.array_agg())
    >>> res
       programming                                        id_array
    0    Beginner           (40,38,3,22,1,39,2,21,34,32,35,31,29)
    1    Advanced  (19,26,20,18,8,6,25,17,15,11,9,28,16,14,10,27)
    2      Novice                 (36,7,5,24,37,4,23,13,30,33,12)
 
    # Example 4: Get the array of values in 'id' column for each combination of 'masters'
    #            and 'programming' level ordered by 'id' column.
    >>> res = df.groupby(["masters", "programming"]).assign(id_array=df.id.array_agg(value_expr = 'id'))
    >>> res
      masters programming                       id_array
    0      no    Advanced        (8,9,10,11,16,17,25,28)
    1      no    Beginner                      (3,21,35)
    2     yes    Advanced       (6,14,15,18,19,20,26,27)
    3     yes    Beginner  (1,2,22,29,31,32,34,38,39,40)
    4     yes      Novice                    (4,7,23,30)
    5      no      Novice          (5,12,13,24,33,36,37)