nerdexam
Databricks

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…

Performing DataFrame Transformations

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
    1. withColumn
  • B
    1. withColumn
  • C
    1. withColumn
  • D
    1. withColumn
  • E
    1. newColumn

How the community answered

(40 responses)
  • A
    3% (1)
  • B
    3% (1)
  • C
    95% (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

#Spark DataFrames#DataFrame Transformations#withColumn#Literal Functions

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice