Featured Article

Master PySpark Least Function for Optimal Performance

Kenneth Jul 13, 2026

PySpark, the Python API for Apache Spark, offers a robust suite of data processing functions, with the 'least' function being one of the many useful tools in the developer's toolkit. The least function, part of the DataFrame API, is used to return the minimum (least) value in a group of certain conditions, making it invaluable for aggregating and analyzing data.

How to Turn Python Functions into PySpark Functions (UDF)
How to Turn Python Functions into PySpark Functions (UDF)

In this comprehensive guide, we'll dive into the world of PySpark's 'least' function, exploring its syntax, usage, and real-world applications. Whether you're a seasoned data scientist or just starting with PySpark, this article will help you master this essential function.

Access previous rows with LAG window function ⏮️
Access previous rows with LAG window function ⏮️

Understanding the PySpark 'least' Function

The least function is an aggregator in PySpark, similar to min, max, and avg, but with a distinctive feature. Unlike its counterparts that return a single value, least returns a row, making it ideal for scenarios where you need more than just a single value as output. This function is particularly useful when you want to retrieve the entire row with the minimum value instead of just the minimum value itself.

PySpark Internals - Spark
PySpark Internals - Spark

At its core, the least function operates on DataFrames and allows you to specify which column(s) to select for the minimum value operation. The function then returns the first row (in case of ties) with the minimum value in the selected columns.

Syntax and Basic Usage

PySpark Tutorial for Beginners
PySpark Tutorial for Beginners

The basic syntax of the least function is df.least(col1, col2, ...), where df is your DataFrame and col1, col2, ... are the column names you want to apply the 'least' operation on. The function returns a new DataFrame with the minimum rows based on the specified columns.

Here's a simple example illustrating its usage:

from pyspark.sql import SparkSession

spark = SparkSession.builder.appName('leastFunction').getOrCreate()

# Create a simple DataFrame
data = [(1, "a", 10), (2, "b", 20), (1, "c", 30), (2, "d", 40)]
df = spark.createDataFrame(data, ["id", "name", "value"])

# Apply least function on 'value' column
min_row = df.least('value')

min_row.show()

Handling Ties

PySpark random sample
PySpark random sample

In cases where multiple rows have the same minimum value, the least function returns the first row it encounters with that value. If you need to handle ties differently, you'll need to use additional methods or functions to sort and filter your data accordingly.

For instance, you can use the orderBy function to sort the data in ascending order and then filter using the condition col('value') <= min_value. Here's an example:

min_value = df.agg({'value': 'min'}).first()[0]
min_rows = df.orderBy('value').where('value <= ' + str(min_value)).limit(10).show()

Real-World Applications

How to set up PySpark for your Jupyter notebook
How to set up PySpark for your Jupyter notebook

The least function finds its use cases in various scenarios, making it a versatile tool in a data scientist's arsenal. Here, we explore a couple of real-world applications:

Finding the Cheapest Product

pyspark least function
pyspark least function
Essential Pyspark For Scalable Data Analytics: A Beginner'S Guide To Harnessing The Power And Ease Of Pyspark 3 - 9781800568877
Essential Pyspark For Scalable Data Analytics: A Beginner'S Guide To Harnessing The Power And Ease Of Pyspark 3 - 9781800568877
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
pyspark least function
two different types of pikachu are shown in this graphic diagram, with the same type
two different types of pikachu are shown in this graphic diagram, with the same type
Mastering PySpark Joins, Filters, and GroupBys: A Comprehensive Guide
Mastering PySpark Joins, Filters, and GroupBys: A Comprehensive Guide

Suppose you have an e-commerce dataset with columns such as 'product', 'price', and 'stock'. Using the least function, you can easily find the cheapest product(s) in stock:

First, create the DataFrame:

data = [(...), (...), ...]  # Your e-commerce data
df = spark.createDataFrame(data, ["product", "price", "stock"])

Then, use the least function to find the cheapest product(s):

cheapest_product = df.filter('stock > 0').least('price')
cheapest_product.show()

Finding the Row with the Earliest Timestamp

In a dataset with a 'timestamp' column, you might want to find the first row (i.e., the earliest timestamp). The least function, when applied to the 'timestamp' column, can give you exactly that:

Assuming you have a DataFrame df with a 'timestamp' column:

earliest_row = df.least('timestamp')
earliest_row.show()

In the dynamic world of data analysis, the PySpark 'least' function proves its mettle time and again, providing a robust and intuitive way to aggregate data. Mastering this function will enable you to tackle a wide range of data analysis tasks more efficiently.

So, the next time you're faced with a dataset that requires finding the 'least' of something, just remember - PySpark's 'least' function has got you covered. Happy coding!