DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #95
Which of the following code blocks returns a new DataFrame with a new column customerSatisfactionAbs that is the absolute value of column customerSatisfaction in DataFrame storesDF? Note that column…
The correct answer is A. storesDF.withColumn("customerSatisfactionAbs", abs(col("customerSatisfaction"))). The correct syntax for withColumn is: DataFrame.withColumn(colName: String, col: Column). Option A correctly passes the new column name as a string ("customerSatisfactionAbs") and a Column expression (abs(col("customerSatisfaction"))). Option B uses withColumnRenamed, which…
Question
Which of the following code blocks returns a new DataFrame with a new column customerSatisfactionAbs that is the absolute value of column customerSatisfaction in DataFrame storesDF? Note that column customerSatisfactionAbs is not in the original DataFrame storesDF.
Options
- AstoresDF.withColumn("customerSatisfactionAbs", abs(col("customerSatisfaction")))
- BstoresDF.withColumnRenamed("customerSatisfactionAbs", abs(col("customerSatisfaction")))
- CstoresDF.withColumn(col("customerSatisfactionAbs", abs(col("customerSatisfaction")))
- DstoresDF.withColumn("customerSatisfactionAbs", abs(col(customerSatisfaction)))
- EstoresDF.withColumn("customerSatisfactionAbs", abs("customerSatisfaction"))
How the community answered
(22 responses)- A91% (20)
- B5% (1)
- C5% (1)
Explanation
The correct syntax for withColumn is: DataFrame.withColumn(colName: String, col: Column). Option A correctly passes the new column name as a string ("customerSatisfactionAbs") and a Column expression (abs(col("customerSatisfaction"))). Option B uses withColumnRenamed, which renames an existing column - it does not add a new one. Option C has a syntax error (missing closing parenthesis for col()). Option D passes customerSatisfaction without quotes, treating it as a variable rather than a column name string. Option E passes a plain string to abs() instead of a Column object - abs() requires a Column, not a String.
Topics
Community Discussion
No community discussion yet for this question.