nerdexam
Databricks

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

Which of the following code blocks returns a 15 percent sample of rows from DataFrame storesDF without replacement?

The correct answer is B. storesDF.sample(fraction = 0.15). The PySpark DataFrame sample() method signature is sample(withReplacement=False, fraction=None, seed=None). The default for withReplacement is False, so calling storesDF.sample(fraction=0.15) returns a 15% sample WITHOUT replacement - exactly what the question asks for. Choice…

Performing Data Transformations using Spark DataFrames

Question

Which of the following code blocks returns a 15 percent sample of rows from DataFrame storesDF without replacement?

Options

  • AstoresDF.sample(True, fraction = 0.15)
  • BstoresDF.sample(fraction = 0.15)
  • CstoresDF.sampleBy(fraction = 0.15)
  • DstoresDF.sample(fraction = 0.10)
  • EstoresDF.sample()

How the community answered

(35 responses)
  • A
    3% (1)
  • B
    89% (31)
  • C
    6% (2)
  • E
    3% (1)

Explanation

The PySpark DataFrame sample() method signature is sample(withReplacement=False, fraction=None, seed=None). The default for withReplacement is False, so calling storesDF.sample(fraction=0.15) returns a 15% sample WITHOUT replacement - exactly what the question asks for. Choice A passes True as the first positional argument, enabling sampling WITH replacement, which is wrong. Choice C uses sampleBy(), which is for stratified sampling and requires a column and fractions dictionary. Choice D uses fraction=0.10, which gives a 10% sample, not 15%. Choice E omits the required fraction argument entirely.

Topics

#Spark DataFrame#Sampling#PySpark API#Data Transformation

Community Discussion

No community discussion yet for this question.

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