通过


sum (GroupedData)

计算每个组的每个数值列的总和。

Syntax

sum(*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 sum of the age in each group.
df.groupBy("name").sum("age").sort("name").show()
# +-----+--------+
# | name|sum(age)|
# +-----+--------+
# |Alice|       5|
# |  Bob|      15|
# +-----+--------+

# Calculate the sum of the age and height in all data.
df.groupBy().sum("age", "height").show()
# +--------+-----------+
# |sum(age)|sum(height)|
# +--------+-----------+
# |      20|        440|
# +--------+-----------+