DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #3
Which of the following code blocks returns a new DataFrame from DataFrame storesDF where column storeId is of the type string?
The correct answer is B. storesDF.withColumn("storeId, col("storeId").cast(StringType())). In PySpark, casting a column's type is done by calling the .cast() method on a Column object and passing in the target type (e.g., StringType()). The correct pattern is col("columnName").cast(TargetType()). Option B - storesDF.withColumn("storeId"…
Question
Which of the following code blocks returns a new DataFrame from DataFrame storesDF where column storeId is of the type string?
Options
- AstoresDF.withColumn("storeId, cast(col("storeId"), StringType()))
- BstoresDF.withColumn("storeId, col("storeId").cast(StringType()))
- CstoresDF.withColumn("storeId, cast(storeId).as(StringType)
- DstoresDF.withColumn("storeId, col(storeId).cast(StringType)
- EstoresDF.withColumn("storeId, cast("storeId").as(StringType()))
How the community answered
(49 responses)- B90% (44)
- C6% (3)
- D2% (1)
- E2% (1)
Explanation
In PySpark, casting a column's type is done by calling the .cast() method on a Column object and passing in the target type (e.g., StringType()). The correct pattern is col("columnName").cast(TargetType()). Option B - storesDF.withColumn("storeId", col("storeId").cast(StringType())) - follows this pattern exactly. Options A, C, D, and E all use incorrect syntax: some treat cast as a standalone function, others omit the closing quotes on the column name string, and others forget to instantiate the type with parentheses (e.g., StringType vs StringType()). The first argument to withColumn must also be a properly quoted string.
Topics
Community Discussion
No community discussion yet for this question.