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

Currently there are no comments in this discussion, be the first to comment!


Save Cancel