search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

PySpark DataFrame | sort method

Machine Learning
chevron_right
PySpark
chevron_right
Documentation
chevron_right
PySpark DataFrame
schedule Jul 1, 2022
Last updated
local_offer PySpark
Tags

PySpark DataFrame's sort(~) method returns a new DataFrame with the rows sorted based on the specified columns.

Parameters

1. cols | string or list or Column

The columns by which to sort the rows.

2. ascending | boolean or list | optional

Whether to sort in ascending or descending order. By default, ascending=True.

Return Value

A PySpark DataFrame.

Examples

Consider the following PySpark DataFrame:

df = spark.createDataFrame([["Alex", 30], ["Bob", 20], ["Cathy", 20]], ["name", "age"])
df.show()
+-----+---+
| name|age|
+-----+---+
| Alex| 30|
| Bob| 20|
|Cathy| 20|
+-----+---+

Sorting a PySpark DataFrame in ascending order by a single column

To sort our PySpark DataFrame by the age column in ascending order:

df.sort("age").show() # ascending=True
+-----+---+
| name|age|
+-----+---+
|Cathy| 20|
| Bob| 20|
| Alex| 30|
+-----+---+

We could also use sql.functions to refer to the column:

import pyspark.sql.functions as F
df.sort(F.col("age")).show()
+-----+---+
| name|age|
+-----+---+
|Cathy| 20|
| Bob| 20|
| Alex| 30|
+-----+---+

Sorting a PySpark DataFrame in descending order by a single column

To sort a PySpark DataFrame by the age column in descending order:

df.sort("age", ascending=False).show()
+-----+---+
| name|age|
+-----+---+
| Alex| 30|
| Bob| 20|
|Cathy| 20|
+-----+---+

Sorting a PySpark DataFrame by multiple columns

To sort a PySpark DataFrame by the age column first, and then by the name column both in ascending order:

df.sort(["age", "name"]).show()
+-----+---+
| name|age|
+-----+---+
| Bob| 20|
|Cathy| 20|
| Alex| 30|
+-----+---+

Here, Bob and Cathy appear before Alex because their age (20) is smaller. Bob then comes before Cathy because B comes before C.

We can also pass a list of booleans to specify the desired ordering of each column:

df.sort(["age", "name"], ascending=[True, False]).show()
+-----+---+
| name|age|
+-----+---+
|Cathy| 20|
| Bob| 20|
| Alex| 30|
+-----+---+

Here, we are first sorting by age in ascending order, and then by name in descending order.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down