nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #9

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…

The correct answer is E. (storesDF.withColumnRenamed("division", "state"). withColumnRenamed(existingName, newName) renames a single column: the first argument is the current column name and the second is the desired new name. To rename two columns, you chain two calls. Option E - storesDF.withColumnRenamed("division"…

Transforming DataFrames

Question

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?

Options

  • A(storesDF.withColumnRenamed(["division", "state"], ["managerName", "managerFullName"])
  • B(storesDF.withColumn("state", col("division"))
  • C(storesDF.withColumn("state", "division")
  • D(storesDF.withColumnRenamed("state", "division")
  • E(storesDF.withColumnRenamed("division", "state")

How the community answered

(47 responses)
  • A
    2% (1)
  • D
    4% (2)
  • E
    94% (44)

Explanation

withColumnRenamed(existingName, newName) renames a single column: the first argument is the current column name and the second is the desired new name. To rename two columns, you chain two calls. Option E - storesDF.withColumnRenamed("division", "state").withColumnRenamed("managerName", "managerFullName") - correctly chains both renames. Option A incorrectly passes lists to withColumnRenamed, which only accepts single strings. Option D has the argument order reversed (new name first, old name second). Options B and C use withColumn, which adds a new column without removing the original - they would leave the old "division" column alongside the new "state" column and only handle one rename.

Topics

#Spark DataFrame API#Column Operations#DataFrame Transformations#Renaming Columns

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice