You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2023/08/30 02:55:51 UTC
[GitHub] [spark] zhengruifeng commented on a diff in pull request #42717: [SPARK-45003][PYTHON][DOCS] Refine docstring of `asc/desc`
zhengruifeng commented on code in PR #42717:
URL: https://github.com/apache/spark/pull/42717#discussion_r1309566338
##########
python/pyspark/sql/functions.py:
##########
@@ -723,27 +750,67 @@ def desc(col: "ColumnOrName") -> Column:
Parameters
----------
col : :class:`~pyspark.sql.Column` or str
- target column to sort by in the descending order.
+ Target column to sort by in the descending order.
Returns
-------
:class:`~pyspark.sql.Column`
- the column specifying the order.
+ The column specifying the sort order.
Examples
--------
- Sort by the column 'id' in the descending order.
+ Example 1: Sort DataFrame by 'id' column in descending order.
- >>> spark.range(5).orderBy(desc("id")).show()
- +---+
- | id|
- +---+
- | 4|
- | 3|
- | 2|
- | 1|
- | 0|
- +---+
+ >>> from pyspark.sql.functions import desc
+ >>> df = spark.createDataFrame([(4, 'B'), (3, 'A'), (2, 'C')], ['id', 'value'])
+ >>> df.sort(desc("id")).show()
+ +---+-----+
+ | id|value|
+ +---+-----+
+ | 4| B|
+ | 3| A|
+ | 2| C|
+ +---+-----+
+
+ Example 2: Use `desc` in `orderBy` function to sort the DataFrame.
+
+ >>> from pyspark.sql.functions import desc
+ >>> df = spark.createDataFrame([(4, 'B'), (3, 'A'), (2, 'C')], ['id', 'value'])
+ >>> df.orderBy(desc("value")).show()
+ +---+-----+
+ | id|value|
+ +---+-----+
+ | 2| C|
+ | 4| B|
+ | 3| A|
+ +---+-----+
+
+ Example 3: Combine `asc` with `desc` to sort by multiple columns.
+
+ >>> from pyspark.sql.functions import asc, desc
Review Comment:
yes, @LuciferYang what about making a follow up to apply `import pyspark.sql.functions as sf` (to be consistent with the quick start example) ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org