Aggregate a Table
Part of the Fundamentals section of Coddy's PySpark journey. Lesson 10 of 18.
An aggregation combines many rows into a summary. df.agg(F.sum("points").alias("total")) returns a one-row DataFrame. alias gives the result column a useful name.
scores.agg(
F.sum("points").alias("total"),
F.count("*").alias("rows")
)sum ignores null inputs. When no non-null values exist, its result is null. count("*") counts every row, whereas count("points") counts only non-null points.
Challenge
EasyReturn one row containing the sum of amount, named total. Keep Spark’s null result when the input is empty or every amount is missing.
Write transform(df) and return a Spark DataFrame. The checker supplies df with schema amount int. The session and F (PySpark functions) are already available. Do not print; the checker displays the returned rows. Return columns in this order: total.
Try it yourself
def transform(df):
return df
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Summaries and Joins
Aggregate a TableGroup by a KeyJoin Two TablesQuery with Spark SQLRecap: Team TotalsPractice on your own: Online Python compiler