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:
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:
- def scale(df):
- return df * 2 # Output length = input length
- df.pandas_on_spark.transform_batch(scale)
apply_batch():Flexible Output Length: Can return a DataFrame/Series of any size (e.g., aggregations, group-wise stats).
Example:
- def summary(df):
- return df.mean() # Output length ≠ input length (aggregation)
- df.pandas_on_spark.apply_batch(summary)
Key Difference:
MethodInput/Output LengthUse Case
transform_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_batchallows variable output.
"Same restriction":
False—only
transform_batchenforces length matching.
Key Takeaway:
Use
transform_batchfor 1:1 row transformations.Use
apply_batchfor 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.
