Nota
Capaian ke halaman ini memerlukan kebenaran. Anda boleh cuba mendaftar masuk atau menukar direktori.
Capaian ke halaman ini memerlukan kebenaran. Anda boleh cuba menukar direktori.
Returns the average of the values in a group. An alias of avg.
Syntax
from pyspark.sql import functions as sf
sf.mean(col)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or column name |
Target column to compute on. |
Returns
pyspark.sql.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.mean("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.mean("age")).show()
+--------+
|avg(age)|
+--------+
| 3.0|
+--------+