Databricks Certified Associate Developer for Apache Spark — Question 60
Which of the following code blocks returns a new DataFrame where column division from DataFrame storesDF has been replaced and renamed to column state and column managerName from DataFrame storesDF has been replaced and renamed to column managerFullName?
Answer options
- A. storesDF.withColumnRenamed("division", "state") .withColumnRenamed("managerName", "managerFullName")
- B. storesDF.withColumn("state", "division") .withColumn("managerFullName", "managerName")
- C. storesDF.withColumn("state", col("division")) .withColumn("managerFullName", col("managerName"))
- D. storesDF.withColumnRenamed(Seq("division", "state"), Seq("managerName", "managerFullName"))
- E. storesDF.withColumnRenamed("state", "division") .withColumnRenamed("managerFullName", "managerName")
Correct answer: A
Explanation
The correct answer is A because it properly uses the withColumnRenamed method to rename the specified columns as required. Options B and C incorrectly use withColumn, which does not rename existing columns. Option D incorrectly attempts to rename multiple columns but does so using the wrong method, and option E reverses the renaming of the columns, which is not the desired operation.