Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Databricks Certified Associate Developer for Apache Spark 3.5 Exam - Topic 3 Question 17 Discussion

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?
D) df.orderBy('age', 'salary', ascending=[True, False]).show()
A) df.orderBy(col('age').asc(), col('salary').asc()).show()
B) df.sort('age', 'salary', ascending=[True, True]).show()
C) df.sort('age', 'salary', ascending=[False, True]).show()

Databricks Certified Associate Developer for Apache Spark 3.5 Exam - Topic 3 Question 17 Discussion

Actual exam question for Databricks's Databricks Certified Associate Developer for Apache Spark 3.5 exam
Question #: 17
Topic #: 3
[All Databricks Certified Associate Developer for Apache Spark 3.5 Questions]

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?

Show Suggested Answer Hide Answer
Suggested Answer: D

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


Contribute your Thoughts:

0/2000 characters
Cordie
2 days ago
D makes total sense, I agree with that choice!
upvoted 0 times
...
Cordelia
7 days ago
I think B is wrong, it sorts both in ascending order.
upvoted 0 times
...
Celestina
12 days ago
Option D is the correct one! Sorting by age asc and salary desc.
upvoted 0 times
...
Angelo
18 days 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.
upvoted 0 times
...
Keneth
23 days 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.
upvoted 0 times
...
Janna
28 days ago
I think option D looks familiar; it seems like the right way to specify different sort orders for each column.
upvoted 0 times
...
Erick
1 month ago
I remember sorting DataFrames, but I’m not sure if the orderBy function uses ascending as a parameter.
upvoted 0 times
...

Save Cancel