pyspark.sql.functions.min#

pyspark.sql.functions.min(col)[source]#

Aggregate function: returns the minimum value of the expression in a group.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

The target column on which the minimum value is computed.

Returns
Column

A column that contains the minimum value computed.

Examples

Example 1: Compute the minimum value of a numeric column

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

Example 2: Compute the minimum value of a string column

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

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

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

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

>>> 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()
+-----+----------+
| name|min(value)|
+-----+----------+
|Alice|         1|
|  Bob|         3|
+-----+----------+

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

>>> 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()
+-----------+-----------+
|min(value1)|min(value2)|
+-----------+-----------+
|          1|        100|
+-----------+-----------+