nerdexam
Databricks

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

Which of the following code blocks returns a new DataFrame where column division is the first two characters of column division in DataFrame storesDF?

The correct answer is E. storesDF.withColumn("division", col("division").substr(l, 2)). In PySpark, the Column.substr(startPos, length) method is 1-indexed, meaning position 1 is the first character. To extract the first 2 characters, you use substr(1, 2) - start at position 1, take 2 characters. Answer E uses col('division').substr(l, 2) where the 'l' represents…

Perform DataFrame Transformations

Question

Which of the following code blocks returns a new DataFrame where column division is the first two characters of column division in DataFrame storesDF?

Options

  • AstoresDF.withColumn("division", substr(col("division"), 0, 2))
  • BstoresDF.withColumn("division", susbtr(col("division"), 1, 2))
  • CstoresDF,withColumn("division", col("division").substr(0, 3))
  • DstoresDF.withColumn("division", col("division").substr(0, 2))
  • EstoresDF.withColumn("division", col("division").substr(l, 2))

How the community answered

(19 responses)
  • A
    11% (2)
  • B
    5% (1)
  • E
    84% (16)

Explanation

In PySpark, the Column.substr(startPos, length) method is 1-indexed, meaning position 1 is the first character. To extract the first 2 characters, you use substr(1, 2) - start at position 1, take 2 characters. Answer E uses col('division').substr(l, 2) where the 'l' represents the number 1 (one), making it substr(1, 2), which correctly returns the first two characters. Options A and D use 0 as the start position, which is incorrect (PySpark substr is 1-based). Option B has a typo ('susbtr'). Option C uses a comma instead of a dot before withColumn and starts at position 0 with length 3.

Topics

#DataFrame Transformations#String Functions#PySpark#Column Operations

Community Discussion

No community discussion yet for this question.

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