nerdexam
Databricks

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

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

The correct answer is B. storesDF.sample(true, fraction = 0.1). The PySpark DataFrame.sample(withReplacement, fraction, seed) method takes a boolean as the first argument (True = sample with replacement) and a float fraction as the second argument (0.1 = 10%). Option B - storesDF.sample(True, fraction=0.1) - correctly specifies both…

Manipulating DataFrames in Apache Spark

Question

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

Options

  • AstoresDF.sample(true)
  • BstoresDF.sample(true, fraction = 0.1)
  • CstoresDF.sample(true, fraction = 0.15)
  • DstoresDF.sampleBy(fraction = 0.1)
  • EstoresDF.sample(false, fraction = 0.1)

How the community answered

(52 responses)
  • A
    2% (1)
  • B
    88% (46)
  • D
    6% (3)
  • E
    4% (2)

Explanation

The PySpark DataFrame.sample(withReplacement, fraction, seed) method takes a boolean as the first argument (True = sample with replacement) and a float fraction as the second argument (0.1 = 10%). Option B - storesDF.sample(True, fraction=0.1) - correctly specifies both with-replacement (True) and 10% (0.1). Option A omits the fraction entirely. Option C uses fraction=0.15, which is 15%, not 10%. Option D uses sampleBy, which is for stratified sampling by column values, not a simple percentage sample. Option E uses False as the first argument, meaning sampling without replacement, which contradicts the requirement.

Topics

#Spark DataFrame API#Data Sampling#DataFrame Transformations

Community Discussion

No community discussion yet for this question.

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