pyspark.sql.DataFrame.foreach#

DataFrame.foreach(f)[source]#

Applies the f function to all Row of this DataFrame.

This is a shorthand for df.rdd.foreach().

New in version 1.3.0.

Changed in version 4.0.0: Supports Spark Connect.

Parameters
ffunction

A function that accepts one parameter which will receive each row to process.

Examples

>>> df = spark.createDataFrame(
...     [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> def func(person):
...     print(person.name)
...
>>> df.foreach(func)