Featured Article

Master PySpark withColumn: Ultimate Example Tutorial for Dataframe Transformations

Kenneth Jul 13, 2026

In the dynamic world of big data processing, Apache Spark's PySpark offers a powerful and flexible API for working with DataFrames. One of its key transformations involves adding new columns or modifying existing ones. The `withColumn` transformation plays a pivotal role in this regard, enabling you to manipulate column data based on your requirements.

PySpark DataFrame Transformations: select vs withColumn
PySpark DataFrame Transformations: select vs withColumn

The syntax for using `withColumn` is simple and straightforward. It involves specifying the column name, followed by a function that defines the new or transformed data. This function must take the DataFrame's columns as an input and return a DataFrame's expression. Let's delve into examples demonstrating the use of `withColumn` in PySpark.

PySpark withColumn() Usage with Examples
PySpark withColumn() Usage with Examples

Adding New Columns with `withColumn`

One of the primary uses of `withColumn` is to add new columns to an existing DataFrame. This could be based on existing columns, constants, or even complex transformations.

PySpark – Cast Column Type With Examples
PySpark – Cast Column Type With Examples

Let's start with a simple example. Suppose we have a DataFrame `df` with columns 'A' and 'B', and we want to add a new column 'C' which is the sum of 'A' and 'B'.

Example 1: Adding a New Column

PySpark ArrayType Column With Examples
PySpark ArrayType Column With Examples

We can achieve this using the `withColumn` transformation as follows:

`df.withColumn('C', df['A'] + df['B'])`. Here, we're creating a new column 'C' that's the sum of columns 'A' and 'B'.

Example 2: Adding a Constant Column

Convert PySpark DataFrame Column to Python List
Convert PySpark DataFrame Column to Python List

You can also add a new column with constant values. For instance, to add a new column 'D' with the value 100 for every row, use `df.withColumn('D', lit(100))`. Here, `lit(100)` creates a Column object with the value 100.

Modifying Existing Columns with `withColumn`

`withColumn` isn't only for adding new columns. It can also be used to modify existing columns based on complex transformations.

PySpark – Convert array column to a String
PySpark – Convert array column to a String

Let's say we have a DataFrame `df` with a column 'Age', and we want to categorize 'Age' into 'Young' or 'Old' based on whether it's below or above 30.

Example 3: Modifying an Existing Column

PySpark Drop Rows with NULL or None Values
PySpark Drop Rows with NULL or None Values
PySpark Replace Column Values in DataFrame
PySpark Replace Column Values in DataFrame
PySpark Window Functions
PySpark Window Functions
PySpark Explode Array and Map Columns to Rows - Spark By {Examples}
PySpark Explode Array and Map Columns to Rows - Spark By {Examples}
PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}
PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}
PySpark “when otherwise” usage with example
PySpark “when otherwise” usage with example
PySpark withColumnRenamed to Rename Column on DataFrame - Spark By {Examples}
PySpark withColumnRenamed to Rename Column on DataFrame - Spark By {Examples}
PySpark partitionBy() – Write to Disk Example
PySpark partitionBy() – Write to Disk Example
PySpark How to Filter Rows with NULL Values
PySpark How to Filter Rows with NULL Values

We can use the `when` and `otherwise` functions to define this categorization. Here's how you can do it:

`df.withColumn('Category', when(df['Age'] < 30, 'Young').otherwise('Old'))`. In this statement, 'Category' is the new column, and we're using `when` and `otherwise` to categorize 'Age'.

Example 4: Combining Multiple Columns and Functions

You can also combine multiple columns and functions in a single `withColumn` statement. For instance, suppose we want to create a new column 'NetPay' that's 'Salary' minus 'Tax' plus 'Bonus'. We can do this with:

`df.withColumn('NetPay', (df['Salary'] - df['Tax']) + df['Bonus'])`. Here, we're combining multiple columns and performing various operations to create a new column.

In conclusion, the `withColumn` transformation in PySpark provides a powerful way to add new columns or modify existing ones based on complex transformations. Whether you're adding a new column from existing ones, adding a constant column, or modifying an existing column, `withColumn` offers a concise, readable, and efficient way to do so. With a bit of creativity, you can manipulate your DataFrames to suit your specific needs.