The code block displayed below contains an error. The code block should return a copy of DataFrame transactionsDf where the name of column transactionId has been changed to

The code block displayed below contains an error. The code block should return a copy of DataFrame transactionsDf where the name of column transactionId has been changed to

transactionNumber. Find the error.

Code block:

transactionsDf.withColumn("transactionNumber", "transactionId")
A . The arguments to the withColumn method need to be reordered.
B . The arguments to the withColumn method need to be reordered and the copy() operator should be appended to the code block to ensure a copy is returned.
C . The copy() operator should be appended to the code block to ensure a copy is returned.
D . Each column name needs to be wrapped in the col() method and method withColumn should be replaced by method withColumnRenamed.
E . The method withColumn should be replaced by method withColumnRenamed and the arguments to the method need to be reordered.

Answer: E

Explanation:

Correct code block:

transactionsDf.withColumnRenamed("transactionId", "transactionNumber")

Note that in Spark, a copy is returned by default. So, there is no need to append copy() to the code block.

More info: pyspark.sql.DataFrame.withColumnRenamed ― PySpark 3.1.2 documentation Static notebook | Dynamic notebook: See test 2,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>