---
title: withField
description: "Column.withField method in PySpark: Add or replace a field in a struct column."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/classes/column/withField
---

# withField

Add or replace a field in a struct column.

## Syntax

```python
withField(fieldName, col)
```

## Parameters

| Parameter   | Type   | Description                           |
| ----------- | ------ | ------------------------------------- |
| `fieldName` | str    | Name of the field to add or replace   |
| `col`       | Column | Column expression for the field value |

## Returns

Column

## Examples

```python
from pyspark.sql import Row
from pyspark.sql.functions import lit
df = spark.createDataFrame([Row(a=Row(b=1, c=2))])
df.withColumn('a', df['a'].withField('b', lit(3))).select('a.b').show()
```

```Output
# +---+
# |  b|
# +---+
# |  3|
# +---+
```

```python
df.withColumn('a', df['a'].withField('d', lit(4))).select('a.d').show()
```

```Output
# +---+
# |  d|
# +---+
# |  4|
# +---+
```
