Databricks Certified Machine Learning Associate · Free Practice Question Medium

Question 20

What is the key difference between DataFrame.pandas_on_spark.transform_batch() and DataFrame.pandas_on_spark.apply_batch()?

  • A

    transform_batch and apply_batch are interchangeable.

  • B

    transform_batch requires the length of input and output to be the same, while apply_batch does not have this restriction.

  • C

    apply_batch requires the length of input and output to be the same, while transform_batch does not have this restriction.

  • D

    Both transform_batch and apply_batch have the same length restriction.

Reveal correct answer

Correct answer: B

Explanation

Correct Answer:
transform_batch requires the length of input and output to be the same, while apply_batch does not have this restriction.

Explanation:

  1. Why This Is Correct?

    • transform_batch():

      • Input/Output Length Must Match: The function must return a DataFrame/Series with the same number of rows as the input. Ideal for row-wise transformations (e.g., scaling, filtering).

      • Example:


    • apply_batch():

      • Flexible Output Length: Can return a DataFrame/Series of any size (e.g., aggregations, group-wise stats).

      • Example:


  2. Key Difference:

    MethodInput/Output LengthUse Casetransform_batchMust matchRow-wise operationsapply_batchCan differAggregations, summaries

Why Other Options Are Incorrect?

  • "Interchangeable":

    • They serve different purposes (fixed vs. flexible output size).

  • "apply_batch has length restriction":

    • Incorrect—apply_batch allows variable output.

  • "Same restriction":

    • False—only transform_batch enforces length matching.

Key Takeaway:

  • Use transform_batch for 1:1 row transformations.

  • Use apply_batch for aggregations or variable-length outputs.

Pro Tip: For grouped operations, combine with groupby.apply_batch().

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need