| |
- avg(value_expression)
- DESCRIPTION:
Function returns the arithmetic average of all values in value_expression.
PARAMETERS:
value_expression:
Required Argument.
Specifies a ColumnExpression of a numeric column for which the average
is to be computed.
Format for the argument: '<dataframe>.<dataframe_column>.expression'.
Note:
Nulls are not included in the result computation.
NOTE:
Function accepts positional arguments only.
ALTERNATE NAMES:
1. ave
2. average
EXAMPLES:
# Load the data to run the example.
>>> load_example_data("dataframe", "admissions_train")
>>>
# Create a 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.
# Import func from sqlalchemy to execute avg function.
>>> from sqlalchemy import func
# Create a sqlalchemy Function object.
>>> avg_func_ = func.avg(admissions_train.gpa.expression)
>>>
# Pass the Function object as input to DataFrame.assign().
>>> df = admissions_train.assign(True, avg_gpa_=avg_func_)
>>> print(df)
avg_gpa_
0 3.54175
>>>
# "average" can be used as an alternative function name.
>>> average_func_ = func.average(admissions_train.gpa.expression)
>>>
# Pass the Function object as input to DataFrame.assign().
>>> df = admissions_train.assign(True, average_gpa_ = average_func_)
>>> print(df)
average_gpa_
0 3.54175
>>>
# Example 2: Calculate the average "gpa" for each level of programming.
# Note:
# When assign() is run after DataFrame.groupby(), the function ignores
# the "drop_columns" argument.
>>> admissions_train.groupby("programming").assign(average_gpa_=func.ave(admissions_train.gpa.expression))
programming average_gpa_
0 Advanced 3.615625
1 Novice 3.294545
2 Beginner 3.660000
>>>
|