Microsoft Certified Azure Data Engineer Associate · Free Practice Question Easy
Question 10
Which method for renaming a DataFrame column is incorrect?
-
A
df.alias("timestamp", "dateCaptured") -
B
df.select(col("timestamp").alias("dateCaptured")) -
C
All are incorrect.
-
D
All are correct.
-
E
df.toDF("dateCaptured")
Reveal correct answer
Correct answer: A
Explanation
The DataFrame does not contain an alias method for a column. df.alias("timestamp", "dateCaptured") is an incorrect method for renaming a DataFrame column.
A. The df.alias() method is used to create an alias for a DataFrame, not to rename a specific column. It does not provide a way to rename a column directly.
B. The df.select(col("timestamp").alias("dateCaptured")) method is a correct way to rename a DataFrame column. It selects the "timestamp" column and renames it as "dateCaptured" using the alias() method.
C. This choice is incorrect because not all methods listed are incorrect. Choice C demonstrates the correct way to rename a DataFrame column, so it is not accurate to say that all methods are incorrect.
D. This choice is incorrect because not all methods listed are correct. Choice A is not a valid method for renaming a DataFrame column, so it is inaccurate to say that all methods are correct.
E. The df.toDF() method is used to create a new DataFrame with the specified column names. It does not rename an existing column in the DataFrame.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
