QuestionQ182

VerifiedData Processing & Transformations

A data engineer receives a PySpark DataFrame named df containing the columns product and revenue. The data engineer must calculate complex aggregations to identify every product’s total revenue, average revenue, and transaction count.

Which code snippet should the data engineer use?

Explanation

groupBy("product").agg(...) calculates aggregates independently for each product. PySpark GroupedData.agg accepts aggregate Column expressions, so F.sum("revenue"), F.avg("revenue"), and F.count("*") produce the required total, average, and row count; alias assigns the specified result-column names.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!