search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

PySpark DataFrame | sort method

schedule Aug 12, 2023
Last updated
local_offer
PySpark
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...