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
Cammy
22 days ago
B doesn't sort salary correctly. D is the right choice!
upvoted 0 times
...
Lemuel
27 days ago
I feel confused. Why not B? It seems simpler.
upvoted 0 times
...
Rebecka
1 month ago
I think option D is correct. It sorts age ascending and salary descending.
upvoted 0 times
...
Amber
1 month ago
Not sure about D, seems too simple for complex sorting.
upvoted 0 times
...
Florinda
1 month ago
Wait, can you really sort like that in one line?
upvoted 0 times
...
Cordie
2 months ago
D makes total sense, I agree with that choice!
upvoted 0 times
...
Cordelia
2 months ago
I think B is wrong, it sorts both in ascending order.
upvoted 0 times
...
Celestina
2 months ago
Option D is the correct one! Sorting by age asc and salary desc.
upvoted 0 times
...
Angelo
2 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.
upvoted 0 times
...
Keneth
2 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.
upvoted 0 times
...
Janna
2 months 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
3 months ago
I remember sorting DataFrames, but I’m not sure if the orderBy function uses ascending as a parameter.
upvoted 0 times
...

Save Cancel