DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #179
Which of the following code blocks returns a new DataFrame where column managerNameLength is the number of characters in column managerName in DataFrame storesDF? Assume DataFrame storesDF is the…
The correct answer is A. storesDF.withColumn("managerNameLength", length(col("managerName"))). length(col('managerName')) is the correct PySpark syntax. length() is a built-in Spark SQL function imported from pyspark.sql.functions, and it accepts a Column object as its argument. col('managerName') produces a proper Column object. Choice B passes a plain string instead of…
Question
Which of the following code blocks returns a new DataFrame where column managerNameLength is the number of characters in column managerName in DataFrame storesDF? Assume DataFrame storesDF is the only defined language variable.
Options
- AstoresDF.withColumn("managerNameLength", length(col("managerName")))
- BstoresDF.withColumn("managerNameLength", length("managerName"))
- CstoresDF.withColumn("managerNameLength", col("managerName").length())
- DstoresDF.withColumn("managerNameLength", stringLength(col("managerName")))
- EstoresDF.withColumn("managerNameLength", length(managerName))
How the community answered
(51 responses)- A80% (41)
- B4% (2)
- C2% (1)
- D2% (1)
- E12% (6)
Explanation
length(col('managerName')) is the correct PySpark syntax. length() is a built-in Spark SQL function imported from pyspark.sql.functions, and it accepts a Column object as its argument. col('managerName') produces a proper Column object. Choice B passes a plain string instead of a Column. Choice C uses col('managerName').length() which is not a valid Column method in PySpark. Choice D uses stringLength() which does not exist. Choice E references managerName as an unquoted bare variable, which would cause a NameError.
Topics
Community Discussion
No community discussion yet for this question.