
Answer-first summary for fast verification
Answer: storesDF.withColumn("modality", lit("PHYSICAL"))
To create a new DataFrame with a column 'modality' set to the constant string 'PHYSICAL', the correct approach is to use the `lit` function, which converts a literal value into a column. Option C correctly uses `lit("PHYSICAL")` to achieve this. Option A fails because 'PHYSICAL' is not quoted, leading to a NameError as it's treated as a variable name. Option B incorrectly attempts to reference a column named 'PHYSICAL' rather than setting a constant value. Option D is incorrect because `StringType` is used for defining schema types, not for setting column values. Option E is syntactically incorrect because it passes a string directly, which is not a valid Column expression.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
Which of the following code blocks returns a new DataFrame from DataFrame storesDF where column modality contains the constant string "PHYSICAL"? Assume storesDF is the only predefined variable.
A
storesDF.withColumn("modality", lit(PHYSICAL))
B
storesDF.withColumn("modality", col("PHYSICAL"))
C
storesDF.withColumn("modality", lit("PHYSICAL"))
D
storesDF.withColumn("modality", StringType("PHYSICAL"))
E
storesDF.withColumn("modality", "PHYSICAL")