DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #53
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 A. storesDF.withColumnRenamed("division", "state"). To rename columns in PySpark, use withColumnRenamed(existingName, newName). The task requires two renames: 'division' → 'state' and 'managerName' → 'managerFullName'. Option A chains two withColumnRenamed calls: storesDF.withColumnRenamed('division'…
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
- AstoresDF.withColumnRenamed("division", "state")
- BstoresDF.withColumn("state", "division")
- CstoresDF.withColumn("state", col("division"))
- DstoresDF.withColumnRenamed(Seq("division", "state"), Seq("managerName",
- EstoresDF.withColumnRenamed("state", "division")
How the community answered
(58 responses)- A90% (52)
- B2% (1)
- C2% (1)
- D5% (3)
- E2% (1)
Explanation
To rename columns in PySpark, use withColumnRenamed(existingName, newName). The task requires two renames: 'division' → 'state' and 'managerName' → 'managerFullName'. Option A chains two withColumnRenamed calls: storesDF.withColumnRenamed('division', 'state').withColumnRenamed('managerName', 'managerFullName'). Option B and C use withColumn with a string or Column, which adds/replaces column values but doesn't rename or remove the original column. Option D passes Seq() which is Scala syntax, not valid PySpark. Option E has the argument order reversed (new name before old name).
Topics
Community Discussion
No community discussion yet for this question.