Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Returns a sort expression for the target column in descending order. This function is used in sort and orderBy functions. Supports Spark Connect.
Syntax
from pyspark.databricks.sql import functions as dbf
dbf.desc(col=<col>)
Parameters
| 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 sort order.
Examples
Example 1: Sort DataFrame by 'id' column in descending order.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(4, 'B'), (3, 'A'), (2, 'C')], ['id', 'value'])
df.sort(dbf.desc("id")).show()
+---+-----+
| id|value|
+---+-----+
| 4| B|
| 3| A|
| 2| C|
+---+-----+
Example 2: Use desc in orderBy function to sort the DataFrame.
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(4, 'B'), (3, 'A'), (2, 'C')], ['id', 'value'])
df.orderBy(dbf.desc("value")).show()
+---+-----+
| id|value|
+---+-----+
| 2| C|
| 4| B|
| 3| A|
+---+-----+