DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #57
The code block shown below should return a new DataFrame with the mean of column sqft from DataFrame storesDF in column sqftMean. Choose the response that correctly fills in the numbered blanks…
The correct answer is A. 1. agg. To compute a mean and return it in a named column, use agg() with the mean() function (from pyspark.sql.functions) and alias the result. The completed code is: storesDF.agg(mean('sqft').alias('sqftMean')). So blank 1 = agg, blank 2 = mean, blank 3 = 'sqft'. Option A correctly…
Question
The code block shown below should return a new DataFrame with the mean of column sqft from DataFrame storesDF in column sqftMean. Choose the response that correctly fills in the numbered blanks within the code block to complete this task. Code block:
storesDF.1(2(3).alias("sqftMean"))
Options
- A
- agg
- B
- withColumn
- C
- agg
- D
- mean
- E
- agg
How the community answered
(39 responses)- A77% (30)
- B3% (1)
- C8% (3)
- D10% (4)
- E3% (1)
Explanation
To compute a mean and return it in a named column, use agg() with the mean() function (from pyspark.sql.functions) and alias the result. The completed code is: storesDF.agg(mean('sqft').alias('sqftMean')). So blank 1 = agg, blank 2 = mean, blank 3 = 'sqft'. Option A correctly identifies blank 1 as agg. Option B uses withColumn, which cannot apply aggregate functions meaningfully across an entire DataFrame. Options D uses mean as blank 1, which is not a DataFrame method in this position. mean is the function inside agg, not the top-level DataFrame method.
Topics
Community Discussion
No community discussion yet for this question.