---
title: foreachPartition
description: "DataFrame.foreachPartition method in PySpark: Applies the f function to each partition of this DataFrame."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/classes/dataframe/foreachPartition
---

# foreachPartition

Applies the `f` function to each partition of this DataFrame.

## Syntax

```
foreachPartition(f: Callable[[Iterator[Row]], None])
```

## Parameters

| Parameter | Type     | Description                                                                         |
| --------- | -------- | ----------------------------------------------------------------------------------- |
| `f`       | function | A function that accepts one parameter which will receive each partition to process. |

## Examples

```python
df = spark.createDataFrame(
    [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
def func(itr):
    for person in itr:
        print(person.name)

df.foreachPartition(func)
```
