DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #5
The code block shown below should return a new DataFrame from DataFrame storesDF where column modality is the constant string "PHYSICAL", Assume DataFrame storesDF is the only defined language…
The correct answer is C. 1. withColumn. To add a column with a constant (literal) value in PySpark, you use withColumn paired with the lit() function from pyspark.sql.functions. The completed code should read: storesDF.withColumn("modality", lit("PHYSICAL")). So the four blanks are: 1=withColumn, 2="modality", 3=lit…
Question
The code block shown below should return a new DataFrame from DataFrame storesDF where column modality is the constant string "PHYSICAL", Assume DataFrame storesDF is the only defined language variable. Choose the response that correctly fills in the numbered blanks within the code block to complete this task. Code block:
storesDF. 1(2,3(4))
Options
- A
- withColumn
- B
- withColumn
- C
- withColumn
- D
- withColumn
- E
- newColumn
How the community answered
(40 responses)- A3% (1)
- B3% (1)
- C95% (38)
Explanation
To add a column with a constant (literal) value in PySpark, you use withColumn paired with the lit() function from pyspark.sql.functions. The completed code should read: storesDF.withColumn("modality", lit("PHYSICAL")). So the four blanks are: 1=withColumn, 2="modality", 3=lit, 4="PHYSICAL". Option E is immediately eliminated because newColumn is not a valid Spark DataFrame method. Among the remaining options (all showing withColumn for blank 1), only Option C correctly fills in lit as the function and "PHYSICAL" as the string literal argument. Without lit(), Spark cannot interpret a plain Python string as a constant column value.
Topics
Community Discussion
No community discussion yet for this question.