QuestionQ187

VerifiedData Processing & Transformations

A global retail company sells products across multiple categories (e.g., Electronics, Clothing) and regions (e.g., North, South, East, West). The sales team has provided the data engineer with a PySpark dataframe named sales_df as below and the team wants the data engineer to analyze the sales data to help them make strategic decisions.

Question Image

Calculate the total sales amount for each product category and store the results in a new dataframe called category_sales.

What will generate the expected result of category_sales?

Question Image

Explanation

The correct PySpark syntax to sum sales by category is a groupBy on the "category" column followed by an aggregation using sum("sales_amount").alias("total_sales_amount"): sales_df.groupBy("category").agg(sum("sales_amount").alias("total_sales_amount")). The other options either use invalid method chaining (sum before groupBy, or agg wrapping groupBy) or group by the wrong column (region instead of category). Learn more: Aggregation - PySpark

Community Discussion

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