Sort Rows
Part of the Fundamentals section of Coddy's PySpark journey. Lesson 7 of 18.
A DataFrame has no guaranteed row order unless you request one. orderBy("name") sorts ascending. A descending column expression uses F.col("score").desc().
scores.orderBy(F.col("score").desc(), "name")The example sorts highest score first, then name ascending when scores tie. A second key makes equal scores deterministic. In this exercise, the checker preserves your row order.
Challenge
EasyReturn all rows sorted by score descending. For tied scores, sort name ascending. Names are unique and neither column contains null.
Write transform(df) and return a Spark DataFrame. The checker supplies df with schema name string, score int. The session and F (PySpark functions) are already available. Do not print; the checker displays the returned rows. Return columns in this order: name, score.
Try it yourself
def transform(df):
return df
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Transforming Data
Select ColumnsFilter RowsDerived ColumnsSort RowsMissing ValuesRecap: Stock ReportPractice on your own: Online Python compiler