QuestionQ131

VerifiedData Processing & Transformations

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

Question Image

Calculate the total sales amount for each region and store the results in a new dataframe called region_sales.

Given the expected result:

Question Image

Which code will generate the expected result?

Explanation

To total sales per region in PySpark, you group by the region column and aggregate with agg(sum("sales_amount").alias("total_sales_amount")), which is valid DataFrame syntax and produces a region_sales DataFrame with one row per region. The other options either group by the wrong column, call sum directly on the DataFrame before grouping, or chain groupBy inside agg in an invalid order that PySpark does not support.

Community Discussion

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