pyspark.sql.functions.avg#
- pyspark.sql.functions.avg(col)[source]#
Aggregate function: returns the average of the values in a group.
New in version 1.3.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- col
Column
or str target column to compute on.
- col
- Returns
Column
the column for computed results.
Examples
Example 1: Calculating the average age
>>> import pyspark.sql.functions as sf >>> df = spark.createDataFrame([(1982, 15), (1990, 2)], ["birth", "age"]) >>> df.select(sf.avg("age")).show() +--------+ |avg(age)| +--------+ | 8.5| +--------+
Example 2: Calculating the average age with None
>>> import pyspark.sql.functions as sf >>> df = spark.createDataFrame([(1982, None), (1990, 2), (2000, 4)], ["birth", "age"]) >>> df.select(sf.avg("age")).show() +--------+ |avg(age)| +--------+ | 3.0| +--------+