Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

The pandas API on Spark and pandas UDFs

Databricks Spark Developer Associate · Lesson 18 of 18 · 2:26:28

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.

In this lesson

Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: why the pandas API on Spark, pandas_api, to_spark, and to_pandas, the default index, pandas UDFs with Apache Arrow, and the kinds of pandas UDFs. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

pandas code, billions of rows

Coralpeak's data scientists write pandas code on their laptops, and it breaks as soon as the purchase history no longer fits in memory. They also have scoring functions written with pandas, which must now run on billions of rows. This lesson covers why the pandas API on Spark, working with it, pandas UDFs, and the kinds of pandas UDFs.

Why the pandas API on Spark

pandas is the most popular library for data analysis in Python, but it runs on a single machine, so the data must fit in its memory. The pandas API on Spark, in the pyspark.pandas module, offers the same API, running on Spark, so the same code scales across a cluster. Data scientists keep the syntax they know, while Spark brings its optimizer, its distributed execution, and all the sources it can read. It started as a separate project called Koalas, and is now part of Spark itself.

Working with the pandas API on Spark

pandas_api turns a Spark DataFrame into a pandas on Spark DataFrame, and to_spark goes back, so you can choose the API that suits each step. to_pandas collects the whole result into a regular pandas DataFrame on the driver, so like toPandas, it only suits small results. A pandas on Spark DataFrame has an index, like pandas, and the default distributed sequence index numbers the rows without moving them all to one partition. Avoid the plain sequence index type, which numbers the rows in a single partition, and be careful with operations that need a global order, which can be slow on large data. Under the hood, every operation becomes a Spark plan, so the same optimizations, and the same Spark UI, apply.

pandas UDFs

A pandas UDF, also called a vectorized UDF, receives whole batches of a column as pandas Series, instead of one value at a time. Spark moves the data between the executor and Python with Apache Arrow, in batches of ten thousand rows by default, so it is much faster than a row by row Python UDF. You declare it with the pandas_udf decorator and a return type, and Python type hints tell Spark which kind of function it is. You call it in select or withColumn, like any column function, and register it with spark.udf.register to use it from SQL.

Kinds of pandas UDFs

Series to Series is the most common kind: the function takes one or more Series, and returns a Series of the same length. Iterator of Series to Iterator of Series receives the batches one after another, which helps when each task must first load something expensive, such as a model. Series to scalar returns a single value for each group, so it works as an aggregate function, with groupBy and agg, or over a window. The type hints decide the kind, so they must match what the function really receives and returns.

Recap

Let's recap. pandas runs on a single machine, while the pandas API on Spark, in pyspark.pandas, keeps the same syntax and runs on the cluster. pandas_api and to_spark switch between the two APIs, and to_pandas brings a small result to the driver. A pandas UDF processes whole batches as pandas Series, moved by Apache Arrow, far faster than a row by row UDF. You declare it with pandas_udf, a return type, and type hints, then call it in select, withColumn, or SQL. And the kind depends on the hints: Series to Series, an iterator of Series, or Series to scalar for aggregates.

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.

  1. A data scientist's pandas code fails with memory errors, now that the purchase history has grown to billions of rows. Which change keeps most of the code?

    • Rewrite everything with RDDs
    • Load the data with toPandas() first
    • Import pyspark.pandas as ps, and use it in place of pandas
    • Run pandas on a laptop with more memory
    Show the answer

    Import pyspark.pandas as ps, and use it in place of pandas

    The pandas API on Spark keeps the pandas syntax, while Spark spreads the work across the cluster. “Rewrite everything with RDDs” would mean rewriting all the code, with a lower level API. “Load the data with toPandas() first” would bring all the data back to one machine. And “Run pandas on a laptop with more memory” only delays the problem, since pandas stays on a single machine.

  2. A team has a Spark DataFrame, and wants to run pandas-style code on it, without collecting it to the driver. Which call fits?

    • pandas_api()
    • toPandas()
    • to_spark()
    • collect()
    Show the answer

    pandas_api()

    Pandas_api returns a pandas on Spark DataFrame, which stays distributed. “toPandas()” would collect the whole table to the driver. “to_spark()” would go the other way, from pandas on Spark back to Spark. And “collect()” would bring every row to the driver, as a list.

  3. A scoring function must load a large model once per task, then score every batch of rows. Which kind of pandas UDF fits?

    • Series to Series
    • Series to scalar
    • A row-by-row Python UDF
    • Iterator of Series to Iterator of Series
    Show the answer

    Iterator of Series to Iterator of Series

    The iterator kind receives the batches one after another, so the model is loaded once, before the loop. “Series to Series” would run the whole function for each batch, loading the model every time. “Series to scalar” would return one value per group, as an aggregate. And “A row-by-row Python UDF” would process one row at a time, and lose the speed of Arrow.