nerdexam
Databricks

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

Which of the following operations can be used to rename and replace an existing column in a DataFrame?

The correct answer is B. DataFrame.withColumnRenamed(). DataFrame.withColumnRenamed(existingName, newName) is the correct PySpark method to rename an existing column. It returns a new DataFrame with the specified column renamed. Option A (renamedColumn()) does not exist in the PySpark API. Option C (wlthColumn()) is a typo and does…

Spark DataFrame Operations

Question

Which of the following operations can be used to rename and replace an existing column in a DataFrame?

Options

  • ADataFrame.renamedColumn()
  • BDataFrame.withColumnRenamed()
  • CDataFrame.wlthColumn()
  • Dcol()
  • EDataFrame.newColumn()

How the community answered

(58 responses)
  • A
    2% (1)
  • B
    88% (51)
  • C
    3% (2)
  • E
    7% (4)

Explanation

DataFrame.withColumnRenamed(existingName, newName) is the correct PySpark method to rename an existing column. It returns a new DataFrame with the specified column renamed. Option A (renamedColumn()) does not exist in the PySpark API. Option C (wlthColumn()) is a typo and does not exist. Option D (col()) is a function used to create a Column object reference - it does not operate on DataFrames or rename columns. Option E (newColumn()) does not exist. Note: withColumn() can replace a column's content but requires you to specify the same column name; withColumnRenamed() is specifically designed to rename columns.

Topics

#Spark DataFrame#Column Operations#Data Transformation

Community Discussion

No community discussion yet for this question.

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