pandas UDFs, function APIs, and one model per group
Databricks Machine Learning Professional · Lesson 5 of 22 · 35:32
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: pandas UDFs, the pandas function APIs (applyInPandas, mapInPandas, and cogrouped maps), memory and skew, and training one model per group for thousands of forecasts. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Forty thousand forecasts
Pebblecove wants a demand forecast for each of its forty thousand homes, because every home has its own season, its own guests, and its own price. One global model blurs these differences, and training forty thousand small models one after another on one machine would take days. This lesson covers pandas UDFs and the pandas function APIs, which spread pandas code across the cluster.
pandas UDFs
A pandas UDF, also called a vectorized UDF, runs your Python function on batches of rows, instead of one row at a time. Spark sends each batch to Python through Apache Arrow, as pandas Series, which can make it up to a hundred times faster than a Python UDF that works row by row. The Series to Series form takes and returns Series of the same length, and works with select and withColumn, like a feature computed with pandas or NumPy. The Iterator of Series form receives an iterator of batches, so an expensive setup, like loading a model, happens once for the iterator instead of once per batch. And the Series to scalar form reduces a column to one value, like a custom aggregate after a groupBy.
The pandas function APIs
The pandas function APIs apply a Python function to whole pandas DataFrames, with Arrow for the transfer, and type hints are optional. groupBy, then applyInPandas, implements split, apply, and combine: Spark splits the rows by key, passes each group to your function as one pandas DataFrame, and combines the DataFrames that it returns. mapInPandas maps an iterator of pandas DataFrames to another one, and its output can have any number of rows, while cogroup with applyInPandas brings two DataFrames together by key, and passes both groups to the function. Each call needs the schema of the output, as a string or as a StructType.
Memory and skew
With applyInPandas, all the rows of a group are loaded into memory at once, before your function runs. The setting that caps the size of Arrow batches does not apply to groups, so one very large group can run out of memory, especially when the group sizes are skewed. So keep each group to a size that one worker can hold, for example by grouping on a finer key, or by limiting the history of each group.
One model per group
To train one model per home, group the training data by home, and call applyInPandas with a function that fits a model on its pandas DataFrame. Inside, the function can log the model to MLflow in its own run, and return one row per group, with the home, the run identifier, and a validation metric. Spark runs the groups in parallel on the executors, so the total time drops as the cluster grows. Inference works the same way: group the new rows by home, and the function loads the model of its group, and returns its predictions with the keys. And when a single model suits every group, a pandas UDF that scores batches is simpler.
Recap
Let's recap. pandas UDFs run Python on batches of rows through Arrow, much faster than row by row UDFs. The Iterator of Series form does an expensive setup, like loading a model, once per iterator. applyInPandas runs a function on each group, mapInPandas on batches of any length, and both need an output schema. A whole group sits in memory at once, so skewed groups can run out of memory. And one model per group means applyInPandas for training, with MLflow inside, and applyInPandas again for inference.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A pandas UDF scores rows with a model loaded from MLflow, and loading takes ten seconds. Which form avoids loading it for every batch?
- Series to Series
- Series to scalar
- Iterator of Series to Iterator of Series
- A Python UDF, row by row
Show the answer
Iterator of Series to Iterator of Series
The Iterator of Series form runs its setup once, then loops over the batches of the iterator. “Series to Series” would call the function, and so the loading, for every batch. “Series to scalar” would reduce a column to one value, which is not scoring. And “A Python UDF, row by row” would work row by row, which is slower still.
The team groups bookings by country, and one country holds half of all bookings. One task fails with out of memory. Why?
- Each group is loaded into memory whole
- The Arrow batch size is set too high
- The output schema has too many columns
- It should have used mapInPandas
Show the answer
Each group is loaded into memory whole
ApplyInPandas loads each group into memory as a whole, so the largest country is too big for one worker. “The Arrow batch size is set too high” is wrong, since the batch size setting does not apply to groups at all. “The output schema has too many columns” is wrong, since the output schema decides the columns, not the memory of a group. And “It should have used mapInPandas” is wrong, since mapInPandas has no groups, and would not split this country by itself.
Every day, 40,000 homes need a forecast, and each home has its own model in MLflow. Which pattern fits the daily inference?
- A loop on the driver over the homes
- One global model for every home
- One pandas UDF with a single model
- groupBy home, then applyInPandas
Show the answer
groupBy home, then applyInPandas
ApplyInPandas gives each group to a function that loads that home's model, and predicts for it, in parallel across the cluster. “A loop on the driver over the homes” would loop on the driver, one home after another. “One global model for every home” would replace forty thousand models with one, which loses what each home learned. And “One pandas UDF with a single model” would apply one model to every row, when each home needs its own.