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…
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)- A2% (1)
- B88% (51)
- C3% (2)
- E7% (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
Community Discussion
No community discussion yet for this question.