Menu
CoddyTech

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 icon

Challenge

Easy

Return 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Python compiler