nerdexam
Databricks

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'…

Manipulate DataFrames using the Spark API

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)
  • A
    90% (52)
  • B
    2% (1)
  • C
    2% (1)
  • D
    5% (3)
  • E
    2% (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

#DataFrame Operations#Column Renaming#Spark API#Transformations

Community Discussion

No community discussion yet for this question.

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