Teradata Package for Python Function Reference - mean - 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.window.mean = mean(distinct=False)
DESCRIPTION:
    Function returns the arithmetic average of all values in teradataml
    DataFrame or ColumnExpression over the specified window.
 
PARAMETERS:
    distinct:
        Optional Argument.
        Specifies a flag that decides whether to consider duplicate values
        in a column or not.
        Default Values: False
        Types: bool
 
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.
 
ALTERNATE NAMES:
    1. avg
    2. ave
 
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: Calculate the average value for the 'gpa' column
    #            in a Rolling window, partitioned over 'programming'.
    # Create a Rolling window on 'gpa'.
    >>> window = admissions_train.gpa.window(partition_columns="programming",
    ...                                      window_start_point=-2,
    ...                                      window_end_point=0)
    >>>
    # Execute mean() on the Rolling 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 count() along with
    #       max() window aggregate operations.
    >>> df = admissions_train.assign(mean_gpa=window.mean(), max_gpa=window.max())
    >>> df
       masters   gpa     stats programming  admitted  max_gpa  mean_gpa
    id
    2      yes  3.76  Beginner    Beginner         0     3.95  3.453333
    21      no  3.87    Novice    Beginner         1     3.87  3.710000
    40     yes  3.95    Novice    Beginner         0     3.95  3.773333
    22     yes  3.46    Novice    Beginner         0     3.95  3.760000
    35      no  3.68    Novice    Beginner         1     3.75  3.630000
    29     yes  4.00    Novice    Beginner         0     4.00  3.810000
    11      no  3.13  Advanced    Advanced         1     3.13  3.130000
    28      no  3.93  Advanced    Advanced         1     3.93  3.530000
    16      no  3.70  Advanced    Advanced         1     3.93  3.586667
    8       no  3.60  Beginner    Advanced         1     3.93  3.743333
    >>>
 
    # Example 2: Calculate the average of all the valid columns in teradataml
                 DataFrame, in an Expanding window, partitioned over 'programming'.
    # Create an Expanding window on teradataml DataFrame.
    >>> window = admissions_train.window(partition_columns="programming",
    ...                                  window_start_point=None,
    ...                                  window_end_point=0)
    >>>
    # Execute mean() on the Expanding window.
    >>> df = window.mean()
    >>> df
       masters   gpa     stats programming  admitted  admitted_mean  gpa_mean    id_mean
    id
    2      yes  3.76  Beginner    Beginner         0       0.333333  3.840000   7.000000
    40     yes  3.95    Novice    Beginner         0       0.200000  3.864000  19.000000
    7      yes  2.33    Novice      Novice         1       0.333333  3.608333  17.000000
    22     yes  3.46    Novice    Beginner         0       0.285714  3.587143  17.714286
    27     yes  3.96  Advanced    Advanced         0       0.222222  3.646667  21.111111
    4      yes  3.50  Beginner      Novice         1       0.300000  3.632000  19.400000
    3       no  3.70    Novice    Beginner         1       1.000000  3.700000   3.000000
    25      no  3.96  Advanced    Advanced         1       1.000000  3.830000  14.000000
    17      no  3.83  Advanced    Advanced         1       1.000000  3.830000  15.000000
    13      no  4.00  Advanced      Novice         1       1.000000  3.872500  14.500000
    >>>
 
    # Example 3: Calculate the average of all the valid columns in teradataml DataFrame,
    #            which are grouped by 'masters' and 'gpa' in a Contracting window,
    #            partitioned over 'masters'.
    # Perform group_by() operation on teradataml DataFrame.
    >>> group_by_df = admissions_train.groupby(["masters", "gpa"])
    # Create a Contracting window on teradataml DataFrameGroupBy object.
    >>> window = group_by_df.window(partition_columns="masters",
    ...                             window_start_point=-5,
    ...                             window_end_point=None)
    # Execute mean() on the Contracting window.
    >>> window.mean()
      masters   gpa  gpa_mean
    0     yes  3.79  3.632500
    1     yes  3.50  3.529000
    2     yes  3.96  3.554545
    3     yes  4.00  3.579167
    4     yes  3.90  3.464286
    5     yes  2.33  3.464000
    6      no  3.52  3.265000
    7      no  3.83  3.320000
    8      no  3.82  3.405000
    9      no  3.55  3.438889
    >>>