---
title: try_sum
description: "try\\_sum function in PySpark: Returns the sum calculated from values of a group and the result is null on overflow."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/functions/try_sum
---

# try\_sum

Returns the sum calculated from values of a group and the result is null on overflow.

## Syntax

```python
from pyspark.sql import functions as sf

sf.try_sum(col)
```

## Parameters

| Parameter | Type                                | Description                  |
| --------- | ----------------------------------- | ---------------------------- |
| `col`     | `pyspark.sql.Column` or column name | Target column to compute on. |

## Examples

**Example 1**: Calculating the sum of values in a column

```python
from pyspark.sql import functions as sf
spark.range(10).select(sf.try_sum("id")).show()
```

```Output
+-----------+
|try_sum(id)|
+-----------+
|         45|
+-----------+
```

**Example 2**: Using a plus expression together to calculate the sum

```python
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1, 2), (3, 4)], ["A", "B"])
df.select(sf.try_sum(sf.col("A") + sf.col("B"))).show()
```

```Output
+----------------+
|try_sum((A + B))|
+----------------+
|              10|
+----------------+
```

**Example 3**: Calculating the summation of ages with None

```python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(1982, None), (1990, 2), (2000, 4)], ["birth", "age"])
df.select(sf.try_sum("age")).show()
```

```Output
+------------+
|try_sum(age)|
+------------+
|           6|
+------------+
```
