Skip to main content

rollup

Create a multi-dimensional rollup for the current DataFrame using the specified columns, allowing for aggregation on them.

Syntax

rollup(*cols: "ColumnOrNameOrOrdinal")

Parameters

Parameter

Type

Description

cols

list, str, int or Column

The columns to roll-up by. Each element should be a column name (string) or an expression (Column) or a column ordinal (int, 1-based) or list of them.

Returns

GroupedData: Rolled-up data based on the specified columns.

Notes

A column ordinal starts from 1, which is different from the 0-based __getitem__.

Examples

Python
df = spark.createDataFrame([("Alice", 2), ("Bob", 5)], schema=["name", "age"])

df.rollup("name").count().orderBy("name").show()
# +-----+-----+
# | name|count|
# +-----+-----+
# | NULL| 2|
# |Alice| 1|
# | Bob| 1|
# +-----+-----+

df.rollup("name", df.age).count().orderBy("name", "age").show()
# +-----+----+-----+
# | name| age|count|
# +-----+----+-----+
# | NULL|NULL| 2|
# |Alice|NULL| 1|
# |Alice| 2| 1|
# | Bob|NULL| 1|
# | Bob| 5| 1|
# +-----+----+-----+