nerdexam
Databricks

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

The code block shown below contains an error. The code block is intended to create a single- column DataFrame from Python list years which is made up of integers. Identify the error. Code block…

The correct answer is E. The IntegerType call should not be present - Spark can tell that list years is full of integers. When passing a plain Python list of primitives to spark.createDataFrame(), Spark can infer the schema automatically without requiring an explicit type argument. The IntegerType argument is unnecessary and its presence (especially without parentheses, since IntegerType is a…

PySpark DataFrame Operations

Question

The code block shown below contains an error. The code block is intended to create a single- column DataFrame from Python list years which is made up of integers. Identify the error. Code block:

spark.createDataFrame(years, IntegerType)

Options

  • AThe column name must be specified.
  • BThe years list should be wrapped in another list like [years] to make clear that it is a column rather
  • CThere is no createDataFrame operation in spark.
  • DThe IntegerType call must be followed by parentheses.
  • EThe IntegerType call should not be present - Spark can tell that list years is full of integers.

How the community answered

(39 responses)
  • B
    5% (2)
  • C
    3% (1)
  • D
    3% (1)
  • E
    90% (35)

Explanation

When passing a plain Python list of primitives to spark.createDataFrame(), Spark can infer the schema automatically without requiring an explicit type argument. The IntegerType argument is unnecessary and its presence (especially without parentheses, since IntegerType is a class reference rather than an instance) causes the call to fail. The correct expression is simply spark.createDataFrame(years). If an explicit schema were truly needed, it would require instantiation - IntegerType() - but even then the preferred pattern is to let Spark infer the type from the Python list's contents.

Topics

#PySpark DataFrame API#Schema Inference#createDataFrame#Data Type Handling

Community Discussion

No community discussion yet for this question.

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