

The above two examples return the same output as above. Spark.sql("select employee_name,department,state,salary,age,bonus from EMP ORDER BY department asc").show(truncate=False)
#Reditr sort column view how to
Using Raw SQLīelow is an example of how to sort DataFrame using raw SQL syntax. This yields the below output for all three examples.īesides asc() and desc() functions, PySpark also provides asc_nulls_first() and asc_nulls_last() and equivalent descending functions. From our example, let’s use desc on the state column.ĭf.sort(df.department.asc(),df.sc()).show(truncate=False)ĭf.sort(col("department").asc(),col("state").desc()).show(truncate=False)ĭf.orderBy(col("department").asc(),col("state").desc()).show(truncate=False) If you wanted to specify the sorting by descending order on DataFrame, you can use the desc method of the Column function.

The above three examples return the same output. for exampleĭf.sort(df.department.asc(),df.state.asc()).show(truncate=False)ĭf.sort(col("department").asc(),col("state").asc()).show(truncate=False)ĭf.orderBy(col("department").asc(),col("state").asc()).show(truncate=False) If you wanted to specify the ascending order/sort explicitly on DataFrame, you can use the asc method of the Column function. This returns the same output as the previous section. By default, it orders by ascending.ĭf.orderBy("department","state").show(truncate=False)ĭf.orderBy(col("department"),col("state")).show(truncate=False) PySpark DataFrame also provides orderBy() function to sort on one or more columns. |employee_name|department|state|salary|age|bonus|ĭataFrame sorting using orderBy() function This table sorted by the first department column and then the state column. The above two examples return the same below output, the first one takes the DataFrame column name as a string and the next takes columns in Column type. Df.sort("department","state").show(truncate=False)ĭf.sort(col("department"),col("state")).show(truncate=False)
