Featured Article

Master PySpark Pandas UDF with This Simple Example

Kenneth Jul 13, 2026

When working with big data in Python, Pyspark and Pandas are both powerful tools that often complement each other. Yet, there are times when you might need to blend their capabilities, such as using Pandas UDFs (User Defined Functions) with Pyspark. This guide will walk you through an example of how to do just that.

Pandas plotting guide for beginners
Pandas plotting guide for beginners

Pandas UDFs are used to vectorize functions in PySpark, enabling us to apply Python functions decorated with the @pandas_udf(return-type)(opy) decorator to PySpark DataFrames. They're perfect for leveraging the computational power of PySpark while benefiting from the familiarity and ease of use of Pandas.

an image of a computer screen with the text panda's chat sheet on it
an image of a computer screen with the text panda's chat sheet on it

Setting Up the Environment

Before we dive into the example, make sure you have both PySpark and Pandas installed. If not, you can install them using pip:

the pandas worksheet is filled with information about data and other important things
the pandas worksheet is filled with information about data and other important things

pip install pyspark pandas numpy

Importing Necessary Libraries

Pandas DataFrames overview in Python
Pandas DataFrames overview in Python

Once installed, import the necessary libraries and initialize a SparkSession:

from pyspark.sql import SparkSession from pyspark.sql.functions import pandas_udf import pandas as pd import numpy as np

spark = SparkSession.builder.appName('pyspark_pandas_udf_example').getOrCreate()

an image of a poster with numbers and symbols on the back of it, which reads numfy ii pandas
an image of a poster with numbers and symbols on the back of it, which reads numfy ii pandas

Defining the Pandas UDF

A Pandas UDF is simply a Python function that returns a Pandas Series. To use it in PySpark, we'll need to decorate it with the pandas_udf decorator.

Decorating the Function

Day 26: Pandas in Python for Beginners 🐼📊
Day 26: Pandas in Python for Beginners 🐼📊

Here's an example of a function decorated with pandas_udf:

@pandas_udf(returnType='integer') def square(x): return x ** 2

Overview of Pandas Data Types
Overview of Pandas Data Types
Pandas, Dask or PySpark? What Should You Choose for Your Dataset?
Pandas, Dask or PySpark? What Should You Choose for Your Dataset?
Ultimate Pandas Cheat Sheet for Beginners (2026)
Ultimate Pandas Cheat Sheet for Beginners (2026)
Pandas Cheat Sheet – A Must-Have for Data Analysts & Scientists
Pandas Cheat Sheet – A Must-Have for Data Analysts & Scientists
an info sheet with different types of data in it and the text above it that says 5 must know python - pandas operations for working with data
an info sheet with different types of data in it and the text above it that says 5 must know python - pandas operations for working with data
NumPy vs Pandas — Which One Should You Learn First?
NumPy vs Pandas — Which One Should You Learn First?
Pandas Tutorials: Unlock the Power of Data Analysis 🔍
Pandas Tutorials: Unlock the Power of Data Analysis 🔍
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas Cheat Sheet for Data Analysts (Chapter 3/15) | Python for Data Analysts
Pandas Cheat Sheet for Data Analysts (Chapter 3/15) | Python for Data Analysts

In this example, the function square takes an integer and returns its square. The return type of the UDF is specified as 'integer'.

Applying the UDF to a PySpark DataFrame

The UDF can now be applied directly to a PySpark DataFrame:

df = spark.createDataFrame([(1, ), (2, ), (3, )], schema=['num']) df.select(square('num')).show()

This will create a new DataFrame where each element in the 'num' column has been squared.

Example with Complex Data Types

UDFs can also handle more complex data types:

List of Lists

You can use the pandas утd decorator to process data like lists of lists:

@pandas_udf(returnType='array>') def multiply(xs): return [xs고 ** 2 for xs in xs]

Here, each list within the DataFrame's existing list is squared.

Multidimensional UDF

UDFS can also handle multiple dimensions. Suppose we have a DataFrame with a column of 2D lists, and we want to compute the sum of squares of all numbers in each sub-list:

@pandas_udf(returnType='integer') def multiply_and_sum(xs): return sum(x ** 2 for x in xs)

The UDF is then applied like so:

df.select(multiply_and_sum('num')).show()

In conclusion, using Pandas UDFs with PySpark can provide a powerful way to leverage the computational capabilities of PySpark while maintaining the familiarity of working with Pandas. Isn't it great how Pyspark and Pandas can work together to make big data processing more accessible and efficient? Go ahead and try it in your own projects, and let us know how it goes!