各グループの各数値列の平均値を計算します。
mean は avg の別名です。
構文
avg(*cols)
パラメーター
| パラメーター | タイプ | 説明 |
|---|---|---|
cols |
str | 列名。 数値以外の列は無視されます。 |
返品
DataFrame
例示
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|
# +--------+-----------+