---
title: sum (GroupedData)
description: "GroupedData.sum method in PySpark: Computes the sum for each numeric column for each group."
canonical_url: https://docs.databricks.com/gcp/en/pyspark/reference/classes/groupeddata/sum
---

# sum (GroupedData)

Computes the sum for each numeric column for each group.

## Syntax

```
sum(*cols)
```

## Parameters

| Parameter | Type | Description                                    |
| --------- | ---- | ---------------------------------------------- |
| `cols`    | str  | Column names. Non-numeric columns are ignored. |

## Returns

`DataFrame`

## Examples

```python
df = spark.createDataFrame([
    (2, "Alice", 80), (3, "Alice", 100),
    (5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])

# Group-by name, and calculate the sum of the age in each group.
df.groupBy("name").sum("age").sort("name").show()
# +-----+--------+
# | name|sum(age)|
# +-----+--------+
# |Alice|       5|
# |  Bob|      15|
# +-----+--------+

# Calculate the sum of the age and height in all data.
df.groupBy().sum("age", "height").show()
# +--------+-----------+
# |sum(age)|sum(height)|
# +--------+-----------+
# |      20|        440|
# +--------+-----------+
```
