Skip to main content

desc_nulls_first

Returns a sort expression based on the descending order of the given column name, and null values appear before non-null values. Supports Spark Connect.

Syntax

Python
from pyspark.sql import functions as dbf

dbf.desc_nulls_first(col=<col>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

Target column to sort by in the descending order.

Parameter

Type

Description

col

pyspark.sql.Column or str

Target column to sort by in the descending order.

Returns

pyspark.sql.Column: the column specifying the order.

Examples

Example 1: Sorting a DataFrame with null values in descending order.

Python
from pyspark.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.desc_nulls_first(df.name)).show()
Output
+---+-----+
|age| name|
+---+-----+
| 0| NULL|
| 1| Bob|
| 2|Alice|
+---+-----+

Example 2: Sorting a DataFrame with null values in descending order using column name string.

Python
from pyspark.sql import functions as dbf
df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"])
df.sort(dbf.desc_nulls_first("name")).show()
Output
+---+-----+
|age| name|
+---+-----+
| 0| NULL|
| 1| Bob|
| 2|Alice|
+---+-----+