nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #50

Which of the following code blocks returns 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. storesDF.withColumn("modality", lit("PHYSICAL")). storesDF.withColumn('modality', lit('PHYSICAL')) is correct. lit() creates a Column containing a literal (constant) value - here, the string 'PHYSICAL'. withColumn then adds or replaces the 'modality' column with that constant for every row. Option A uses lit(PHYSICAL) without…

Spark DataFrame Transformations

Question

Which of the following code blocks returns a new DataFrame from DataFrame storesDF where column modality is the constant string "PHYSICAL"? Assume DataFrame storesDF is the only defined language variable.

Options

  • AstoresDF.withColumn("modality", lit(PHYSICAL))
  • BstoresDF.withColumn("modality", col("PHYSICAL"))
  • CstoresDF.withColumn("modality", lit("PHYSICAL"))
  • DstoresDF.withColumn("modality", StringType("PHYSICAL"))
  • EstoresDF.withColumn("modality", "PHYSICAL")

How the community answered

(37 responses)
  • A
    3% (1)
  • B
    5% (2)
  • C
    89% (33)
  • E
    3% (1)

Explanation

storesDF.withColumn('modality', lit('PHYSICAL')) is correct. lit() creates a Column containing a literal (constant) value - here, the string 'PHYSICAL'. withColumn then adds or replaces the 'modality' column with that constant for every row. Option A uses lit(PHYSICAL) without quotes, so PHYSICAL would be interpreted as an undefined Python variable. Option B uses col('PHYSICAL'), which looks for an existing column named PHYSICAL rather than a constant value. Option D uses StringType('PHYSICAL'), but StringType is a DataType class, not a function for creating constant column values. Option E passes a bare Python string, which is not a valid Column expression.

Topics

#Spark DataFrames#withColumn#Literal expressions#Data Transformation

Community Discussion

No community discussion yet for this question.

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