pyspark.sql.DataFrame.rollup¶
-
DataFrame.
rollup
(*cols: ColumnOrName) → GroupedData[source]¶ Create a multi-dimensional rollup for the current
DataFrame
using the specified columns, so we can run aggregation on them.New in version 1.4.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- Returns
GroupedData
Rolled-up data by given columns.
Examples
>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"]) >>> 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| +-----+----+-----+