Note
Kailangan ng pahintulot para ma-access ang page na ito. Maaari mong subukang mag-sign in o magpalit ng mga direktoryo.
Ang pag-access sa pahinang ito ay nangangailangan ng pahintulot. Maaari mong subukang baguhin ang mga direktoryo.
Computes average values for each numeric column for each group.
mean is an alias for avg.
Syntax
avg(*cols)
Parameters
| Parameter | Type | Description |
|---|---|---|
cols |
str | Column names. Non-numeric columns are ignored. |
Returns
DataFrame
Examples
df = spark.createDataFrame([
(2, "Alice", 80), (3, "Alice", 100),
(5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])
# Group-by name, and calculate the mean of the age in each group.
df.groupBy("name").avg('age').sort("name").show()
# +-----+--------+
# | name|avg(age)|
# +-----+--------+
# |Alice| 2.5|
# | Bob| 7.5|
# +-----+--------+
# Calculate the mean of the age and height in all data.
df.groupBy().avg('age', 'height').show()
# +--------+-----------+
# |avg(age)|avg(height)|
# +--------+-----------+
# | 5.0| 110.0|
# +--------+-----------+