
Answer-first summary for fast verification
Answer: spark_df.withColumn('discounted_price', col('price') * 0.8)
The correct answer is **C**. `spark_df.withColumn('discounted_price', col('price') * 0.8)` is the proper way to add a new column in a Spark DataFrame. The `withColumn` method is used for adding or replacing columns, and the `col` function correctly references the 'price' column. Multiplying by 0.8 reduces the price by 20%. Other options are incorrect due to syntax errors or misuse of Spark DataFrame methods.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
Which code snippet correctly adds a new column 'discounted_price' to a Spark DataFrame 'spark_df', calculating it as 20% less than the 'price' column?
A
spark_df.addColumn('discounted_price', spark_df['price'] * 0.8)
B
ALTER TABLE spark_df ADD COLUMN discounted_price AS price * 0.8
C
spark_df.withColumn('discounted_price', col('price') * 0.8)
D
spark_df['discounted_price'] = spark_df['price'] * 0.8
E
spark_df.withColumn('discounted_price', spark_df('price') * 0.8)