DataFrame operations to retrieve the first element in a group in PySpark

PySpark @

PySpark’s first function is a part of the pyspark.sql.functions module. It is used in DataFrame operations to retrieve the first element in a group after data has been grouped using the groupBy function. This function is particularly useful in scenarios where you need to extract an initial record from each group in your dataset. PySpark’s first function is a simple yet powerful tool for data extraction in grouped datasets.

Why Use the First Function?

The first function is invaluable when you need to simplify large datasets by extracting key pieces of data. It is often used in summarizing data, reporting, and analytics, where the initial or representative data of each group is critical for insights.

Practical Example with Real Data


To demonstrate the use of the first function in PySpark, we will consider a simple dataset containing names and associated scores.

Creating a DataFrame: We will create a DataFrame with names and scores.

from pyspark.sql import SparkSession
from pyspark.sql.functions import first
spark = SparkSession.builder.appName("FirstExample").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 First Function: We will use the first function to extract the first score from our dataset.

df_grouped = df.groupBy().agg(first("Score").alias("FirstScore"))


|        95|

The output of the above code will display the first score from the dataset.

Spark important urls to refer

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