Skip to main content

min

Returns the minimum value of the expression in a group.

Syntax

Python
from pyspark.sql import functions as sf

sf.min(col)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or column name

The target column on which the minimum value is computed.

Parameter

Type

Description

col

pyspark.sql.Column or column name

The target column on which the minimum value is computed.

Returns

pyspark.sql.Column: A column that contains the minimum value computed.

Examples

Example 1: Compute the minimum value of a numeric column

Python
import pyspark.sql.functions as sf
df = spark.range(10)
df.select(sf.min(df.id)).show()
Output
+-------+
|min(id)|
+-------+
| 0|
+-------+

Example 2: Compute the minimum value of a string column

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([("Alice",), ("Bob",), ("Charlie",)], ["name"])
df.select(sf.min("name")).show()
Output
+---------+
|min(name)|
+---------+
| Alice|
+---------+

Example 3: Compute the minimum value of a column with null values

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([(1,), (None,), (3,)], ["value"])
df.select(sf.min("value")).show()
Output
+----------+
|min(value)|
+----------+
| 1|
+----------+

Example 4: Compute the minimum value of a column in a grouped DataFrame

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame([("Alice", 1), ("Alice", 2), ("Bob", 3)], ["name", "value"])
df.groupBy("name").agg(sf.min("value")).show()
Output
+-----+----------+
| name|min(value)|
+-----+----------+
|Alice| 1|
| Bob| 3|
+-----+----------+

Example 5: Compute the minimum value of a column in a DataFrame with multiple columns

Python
import pyspark.sql.functions as sf
df = spark.createDataFrame(
[("Alice", 1, 100), ("Bob", 2, 200), ("Charlie", 3, 300)],
["name", "value1", "value2"])
df.select(sf.min("value1"), sf.min("value2")).show()
Output
+-----------+-----------+
|min(value1)|min(value2)|
+-----------+-----------+
| 1| 100|
+-----------+-----------+