PySpark’s DESC Function: DataFrame operations to sort data in descending order

PySpark @

PySpark, the Python API for Apache Spark, is widely used for its efficiency and ease of use. One of the essential functions in PySpark is the desc function, crucial for sorting data in descending order. This article delves into the nuances of the desc function, offering insights and practical examples to enhance your data manipulation skills.

Understanding PySpark’s DESC Function

What is PySpark’s DESC Function?

PySpark’s desc function is used in DataFrame operations to sort data in descending order. It’s a method that can be applied to a DataFrame column, altering the way data is organized. This function is particularly useful when you need to analyze top-performing elements in a dataset, such as the highest sales, the most active users, or other similar metrics.

Why Use the DESC Function?

Sorting data is a fundamental aspect of data analysis. By using the desc function, analysts and data scientists can quickly identify high-value or high-frequency items, making it easier to draw meaningful conclusions and make informed decisions.

Practical Example with Real Data


To demonstrate the use of the desc function in PySpark, we’ll consider a simple dataset containing names and scores. Our dataset includes the following names: Sachin, Manju, Ram, Raju, David, Freshers_in, and Wilson.

Step-by-Step Implementation

  1. Setting Up PySpark Environment: Before diving into the example, ensure that PySpark is installed and properly set up in your environment.
  2. Creating a DataFrame: We’ll begin by creating a DataFrame with the names and an associated score for each.
from pyspark.sql import SparkSession
from pyspark.sql.functions import desc
spark = SparkSession.builder.appName("descExample").getOrCreate()
data = [("Sachin", 95), ("Manju", 88), ("Ram", 76), 
        ("Raju", 89), ("David", 92), ("Freshers_in", 65), ("Wilson", 78)]
columns = ["Name", "Score"]
df = spark.createDataFrame(data, columns)

Applying the DESC Function:

Now, we’ll use the desc function to sort the data by scores in descending order.

df_sorted = df.orderBy(desc("Score"))


|       Name|Score|
|     Sachin|   95|
|      David|   92|
|       Raju|   89|
|      Manju|   88|
|     Wilson|   78|
|        Ram|   76|
|Freshers_in|   65|

Spark important urls to refer

  1. Spark Examples
  2. PySpark Blogs
  3. Bigdata Blogs
  4. Spark Interview Questions
  5. Official Page
Author: user