
Answer-first summary for fast verification
Answer: storesDF.withColumn(“customerSatisfactionAbs”, abs(col(“customerSatisfaction”)))
The correct answer is A. The `withColumn` method is used to add a new column by specifying the column name as a string and a Column expression. Option A correctly applies the `abs` function to the existing column `customerSatisfaction`. Options B uses `withColumnRenamed`, which renames an existing column but does not compute a new one. Option C has incorrect syntax for `withColumn`. Option D omits quotes around the column name in `col(customerSatisfaction)`, causing an error. Option E passes a string directly to `abs` instead of a Column object.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
Which of the following code blocks creates a new DataFrame by adding a column named customerSatisfactionAbs containing the absolute values from the customerSatisfaction column in DataFrame storesDF? The customerSatisfactionAbs column does not exist in the original storesDF.
A
storesDF.withColumn(“customerSatisfactionAbs”, abs(col(“customerSatisfaction”)))
B
storesDF.withColumnRenamed(“customerSatisfactionAbs”, abs(col(“customerSatisfaction”)))
C
storesDF.withColumn(col(“customerSatisfactionAbs”, abs(col(“customerSatisfaction”))))
D
storesDF.withColumn(“customerSatisfactionAbs”, abs(col(customerSatisfaction)))
No comments yet.