Featured Article

Master PySpark UDF Example for Multiple Columns Efficiently

Kenneth Jul 13, 2026

Spark is a robust processing engine by Apache that allows for parallel data processing. One of its powerful features is the ability to define User-Defined Functions (UDFs), which can operate on DataFrames andıpDataFrames with multiple columns. Today, we'll explore an example of applying UDFs to multiple columns in PySpark.

an empty room with columns and sky in the background
an empty room with columns and sky in the background

Before diving in, ensure you have a basic understanding of PySpark, DataFrames, and UDFs. This will help you follow along smoothly. Let's begin.

the four pillars in front of a building with words above it that read,'the four pillars '
the four pillars in front of a building with words above it that read,'the four pillars '

Defining User-Defined Functions (UDFs)

UDFs are a way to incorporate custom functionality into PySpark operations. They allow us to use code not natively supported by PySpark, like complex mathematical equations, string manipulations, or machine learning models.

the columns are lined up in rows and there is no image on this page to describe
the columns are lined up in rows and there is no image on this page to describe

First, let's import necessary modules and create a simple DataFrame with multiple columns.

Import Modules and Create DataFrame

different types of columns used in construction
different types of columns used in construction

```python from pyspark.sql import functions as F, SparkSession from pyspark.sql.types import DoubleType spark = SparkSession.builder.appName('udf_example').getOrCreate() ```

Let's create a DataFrame named 'df' with three columns: 'Name', 'Age', and 'Salary'.

Create DataFrame with Multiple Columns

the pillars diagram is shown with different colors and symbols on it, including lightbulbs
the pillars diagram is shown with different colors and symbols on it, including lightbulbs

```python data = [('John', 30, 50000), ('Jane', 25, 55000), ('Jim', 35, 60000)] Schema = ['Name', 'Age', 'Salary'] df = spark.createDataFrame(data, Schema) df.show() ```

Applying UDFs to Multiple Columns

UDFs can take multiple input columns and return a single output column. Let's define a UDF that calculates annualSalary by multiplying 'Salary' by 12.

four different types of columns with names on them
four different types of columns with names on them

To use this UDF in PySpark, register it as a UDF and apply it to the 'Salary' column.

Define and Register UDF

a table that has different colored lines on it and the number of items in each row
a table that has different colored lines on it and the number of items in each row
an image of the classical order of architecture and its features, including columns with names on them
an image of the classical order of architecture and its features, including columns with names on them
black and white photo of columns with shadows on them
black and white photo of columns with shadows on them
Architecture And Speculative Masonry
Architecture And Speculative Masonry
an image of a column with measurements on it
an image of a column with measurements on it
a golden vase with an ornate design on it's base, against a white background
a golden vase with an ornate design on it's base, against a white background
column size according to spam, including the number of columns and numbers on each column
column size according to spam, including the number of columns and numbers on each column
a red and white sign with the number of people in each column, which has been placed
a red and white sign with the number of people in each column, which has been placed
an artistic sculpture stands in the middle of a concrete floor with stairs leading up to it
an artistic sculpture stands in the middle of a concrete floor with stairs leading up to it

```python annualSalary = F.udf(lambda x: x*12, returnType=DoubleType()) df = df.withColumn('AnnualSalary', annualSalary('Salary')) ```

To apply it to multiple columns, we need to wrap multiple UDF applications in a single function and apply them within the same withColumn call.

Applying UDF to Multiple Columns

Let's define another UDF that ages the 'Age' column by a specific amount, say 5 years.

```python age_old = F.udf(lambda x: x+5, returnType=DoubleType()) df = df.withColumn('Age_New', age_old('Age')) df.show() ```

Here, we used the 'withColumn' function to add a new column 'Age_New' to our DataFrame.

In PySpark, UDFs can significantly enhance the functionality of DataFrames and Datasets, making data processing tasks more efficient and enabling complex operations that wouldn't otherwise be possible. Mastering UDFs can open up a world of possibilities in data transformation, manipulation, and analysis.

Explore more cases and expand your skills with PySpark UDFs. Practice makes progress! Now that you've seen how to create and use UDFs on multiple columns, you can apply these techniques to your own projects and take your data processing capabilities to the next level.