nerdexam
Databricks

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

The code block shown contains an error. The code block is intended to return a new DataFrame where column sqft from DataFrame storesDF has had its missing values replaced with the value 30,000…

The correct answer is A. The argument to the subset parameter of fill() should be a string column name or a list of string. The na.fill(value, subset) method's subset parameter must be a string column name or a list of string column names - it does not accept a Spark Column object. The code storesDF.na.fill(30000, col("sqft")) passes col("sqft") (a Column object) as the subset argument, which will…

Data Transformation with Spark DataFrames

Question

The code block shown contains an error. The code block is intended to return a new DataFrame where column sqft from DataFrame storesDF has had its missing values replaced with the value 30,000. Identify the error. A sample of DataFrame storesDF is displayed below:

Code block:

storesDF.na.fill(30000, col("sqft"))

Options

  • AThe argument to the subset parameter of fill() should be a string column name or a list of string
  • BThe na.fill() operation does not work and should be replaced by the dropna() operation.
  • Che argument to the subset parameter of fill() should be a the numerical position of the column
  • DThe na.fill() operation does not work and should be replaced by the nafill() operation.
  • EThe na.fill() operation does not work and should be replaced by the fillna() operation.

How the community answered

(56 responses)
  • A
    91% (51)
  • C
    5% (3)
  • D
    2% (1)
  • E
    2% (1)

Explanation

The na.fill(value, subset) method's subset parameter must be a string column name or a list of string column names - it does not accept a Spark Column object. The code storesDF.na.fill(30000, col("sqft")) passes col("sqft") (a Column object) as the subset argument, which will raise a TypeError. The correct call is storesDF.na.fill(30000, ["sqft"]) or storesDF.na.fill(30000, "sqft"). Option A correctly identifies this error. Options B and D are wrong because na.fill() is a valid and working method. Option C is wrong because position-based column identification is not how fill() works. Option E is wrong because na.fill() and fillna() are equivalent aliases in Spark - one does not need to replace the other.

Topics

#PySpark#DataFrame Operations#Missing Data Handling#na.fill

Community Discussion

No community discussion yet for this question.

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