pyspark.sql.functions.desc_nulls_first#
- pyspark.sql.functions.desc_nulls_first(col)[source]#
Sort Function: Returns a sort expression based on the descending order of the given column name, and null values appear before non-null values.
New in version 2.4.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- col
Column
or str target column to sort by in the descending order.
- col
- Returns
Column
the column specifying the order.
Examples
Example 1: Sorting a DataFrame with null values in descending order
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"]) >>> df.sort(sf.desc_nulls_first(df.name)).show() +---+-----+ |age| name| +---+-----+ | 0| NULL| | 1| Bob| | 2|Alice| +---+-----+
Example 2: Sorting a DataFrame with multiple columns, null values in descending order
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame( ... [(1, "Bob", None), (0, None, "Z"), (2, "Alice", "Y")], ["age", "name", "grade"]) >>> df.sort(sf.desc_nulls_first(df.name), sf.desc_nulls_first(df.grade)).show() +---+-----+-----+ |age| name|grade| +---+-----+-----+ | 0| NULL| Z| | 1| Bob| NULL| | 2|Alice| Y| +---+-----+-----+
Example 3: Sorting a DataFrame with null values in descending order using column name string
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([(1, "Bob"), (0, None), (2, "Alice")], ["age", "name"]) >>> df.sort(sf.desc_nulls_first("name")).show() +---+-----+ |age| name| +---+-----+ | 0| NULL| | 1| Bob| | 2|Alice| +---+-----+