A DataFrame df has columns name, age, and salary. The developer needs to sort the DataFrame by age in ascending order and salary in descending order.
Which code snippet meets the requirement of the developer?
Suggested Answer: D
Get Premium Databricks Certified Associate Developer for Apache Spark 3.5 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
To sort a PySpark DataFrame by multiple columns with mixed sort directions, the correct usage is:
python
CopyEdit
df.orderBy('age', 'salary', ascending=[True, False])
age will be sorted in ascending order
salary will be sorted in descending order
The orderBy() and sort() methods in PySpark accept a list of booleans to specify the sort direction for each column.
Documentation Reference: PySpark API - DataFrame.orderBy
12 comments · newest first
CA
Cammy
2 months ago
B doesn't sort salary correctly. D is the right choice!
LE
Lemuel
2 months ago
I feel confused. Why not B? It seems simpler.
RE
Rebecka
3 months ago
I think option D is correct. It sorts age ascending and salary descending.
AM
Amber
3 months ago
Not sure about D, seems too simple for complex sorting.
FL
Florinda
3 months ago
Wait, can you really sort like that in one line?
CO
Cordie
3 months ago
D makes total sense, I agree with that choice!
CO
Cordelia
3 months ago
I think B is wrong, it sorts both in ascending order.
CE
Celestina
3 months ago
Option D is the correct one! Sorting by age asc and salary desc.
AN
Angelo
4 months ago
I feel like option A is close, but I’m pretty sure we need to sort salary in descending order, which it doesn’t do.
KE
Keneth
4 months ago
I practiced something similar last week, but I can't recall if we used "ascending" as a list or just as a single argument.
JA
Janna
4 months ago
I think option D looks familiar; it seems like the right way to specify different sort orders for each column.
ER
Erick
4 months ago
I remember sorting DataFrames, but I’m not sure if the orderBy function uses ascending as a parameter.