Learning PySpark: Finding the Minimum Value of a DataFrame Column


Introduction to Minimum Value Calculation in PySpark


The capacity to perform rapid and efficient statistical aggregation is essential when dealing with large-scale datasets, a key capability delivered by PySpark. When analyzing numerical metrics stored within a distributed DataFrame, determining the minimum value of a specific column is a fundamental requirement. This calculation often serves as a critical first step in exploratory data analysis (EDA) or rigorous data validation processes. Identifying the smallest observed value allows data engineers and analysts to accurately understand the lower bounds of a data distribution, pinpoint potential outliers, and confirm overall data integrity. PySpark offers highly optimized methods for achieving this goal, primarily utilizing built-in functions available in the pyspark.sql.functions module, which are specifically engineered for performance and scalability across distributed clusters.


This article will explore two primary, robust techniques tailored to different analytical needs: calculating the minimum for a single, focused column to retrieve a scalar value, and efficiently calculating minimums simultaneously across multiple columns using columnar functions. Choosing the correct approach depends heavily on the desired output format and the scope of the aggregation. For instance, if the objective is to retrieve a single scalar minimum value for immediate use in subsequent non-Spark calculations, the agg() function combined with the collect() action is often preferred.


Conversely, if the requirement is to display the minimums of several columns alongside each other within a new DataFrame structure, the select() transformation, also leveraging aggregate functions, proves to be the most idiomatic and scalable solution. Understanding the subtleties of these methods is crucial for writing clean, performant, and maintainable data processing code within the Spark ecosystem. Efficiency gains are paramount when dealing with petabytes of information, as they directly reduce execution time and resource consumption. The subsequent sections will detail the implementation of both methods using practical, reproducible code examples.

Prerequisites: Setting Up the Sample PySpark DataFrame


Before diving into the specific calculation methodologies, we must first establish a functional DataFrame containing numerical data suitable for aggregation. The setup code below initializes a SparkSession—the entry point for all Spark functionality—and constructs a simple dataset. This dataset simulates team performance metrics across three hypothetical games. This structure serves as the foundation for all subsequent examples, ensuring that the results demonstrated are easily verifiable against the source data.


The sample DataFrame includes a categorical column (‘team’) and three numerical columns (‘game1’, ‘game2’, ‘game3’), which are the targets for our minimum value calculations. The initialization process involves importing necessary PySpark components, defining the raw data structure as a standard list of rows, and explicitly specifying the column names. This approach guarantees clarity and proper schema definition upon creation.


This standardized method ensures that the data types are correctly inferred or assigned, which is vital for mathematical operations like finding the minimum. The use of spark.createDataFrame(data, columns) is the standard mechanism for transforming local Python collections into distributed Spark DataFrames, thereby preparing the data for large-scale parallel processing across the cluster.

from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()

#define data
data = [['Mavs', 25, 11, 10], 
        ['Nets', 22, 8, 14], 
        ['Hawks', 14, 22, 10], 
        ['Kings', 30, 22, 35], 
        ['Bulls', 15, 14, 12], 
        ['Blazers', 10, 14, 18]] 
  
#define column names
columns = ['team', 'game1', 'game2', 'game3'] 
  
#create dataframe using data and column names
df = spark.createDataFrame(data, columns) 
  
#view dataframe
df.show()

+-------+-----+-----+-----+
|   team|game1|game2|game3|
+-------+-----+-----+-----+
|   Mavs|   25|   11|   10|
|   Nets|   22|    8|   14|
|  Hawks|   14|   22|   10|
|  Kings|   30|   22|   35|
|  Bulls|   15|   14|   12|
|Blazers|   10|   14|   18|
+-------+-----+-----+-----+


As demonstrated by the output above, our sample DataFrame, named df, is successfully created. It contains six rows of data, providing sufficient variation in the numerical columns (game1, game2, game3) to effectively illustrate how the minimum calculation functions operate. We will now proceed to apply the two core aggregation techniques to this data structure, beginning with the method best suited for isolating a single column’s minimum value for use in local Python scripts.

Method 1: Calculating Minimum for a Single Specific Column using agg()


When the analytical need is strictly to find the absolute minimum value within just one column of the DataFrame and retrieve it as a native Python object, utilizing the agg() function is the most direct and idiomatic approach. The agg() function is specifically designed to perform one or more aggregate operations over the entire DataFrame (or over grouped partitions). By importing the necessary functions module, typically aliased as F, we gain access to the min() function, which is applied directly to the specified column name. This operation efficiently returns a new, single-row DataFrame containing the calculated minimum result.


To extract the resulting scalar value from this single-row DataFrame, the standard practice is to chain the agg() operation with the .collect() action, followed by array indexing: .collect()[0][0]. The .collect() action forces the distributed computation to execute and brings the result—which is returned as a list of Row objects—back to the driver program. We then access the first row ([0]) and the first column of that row ([0]) to retrieve the raw minimum value as a native Python type, such as an integer or float.


This extraction technique is vital when the minimum value needs to be immediately utilized in subsequent non-Spark Python logic, or if it must be passed to external systems. While using .collect() for large intermediate DataFrames can lead to memory issues on the driver node, in the context of single-value aggregations like finding the minimum of a column, this approach remains highly efficient and straightforward. It provides the maximum precision and direct access to the scalar result, making it the preferred method for quick, verified statistical checks.

from pyspark.sql import functions as F

#calculate minimum of column named 'game1'
df.agg(F.min('game1')).collect()[0][0]

Method 2: Leveraging select() and min() for Multiple Columns


If the analytical goal requires computing and viewing the minimum values for several columns simultaneously, presenting the results concisely within a new, aggregated DataFrame, the select() transformation coupled with the min() function from pyspark.sql.functions offers a cleaner and more scalable solution. While select() is primarily used for column projection and manipulation, when aggregate functions like min() are passed as arguments to select() without a preceding groupBy() operation, Spark implicitly performs the aggregation across all rows. This effectively collapses the entire DataFrame into a single result row containing all the calculated minimums.


This technique provides a distinct advantage because it keeps the result within the distributed PySpark ecosystem. The resulting single-row DataFrame can then be easily cached, written to disk, or used as input for further distributed transformations without incurring the overhead of pulling large amounts of data back to the local driver program. By explicitly importing the min function, we can apply it directly to the column objects (e.g., df.game1). Each argument passed to df.select() corresponds to a new column in the resulting DataFrame, clearly labeled with the function applied (e.g., min(game1)).

from pyspark.sql.functions import min

#calculate minimum for game1, game2 and game3 columns
df.select(min(df.game1), min(df.game2), min(df.game3)).show()


The output of this method is a tidy, easily readable table summarizing the minimum performance for all specified numerical columns. For batch reporting, parallel aggregation across many metrics, or generating summary statistics that need to be persisted back into the cluster storage, Method 2 is generally preferred over Method 1, as it avoids the driver overhead associated with retrieving the result using .collect(). This approach ensures that we are leveraging Spark’s core strengths as a distributed computing framework.

Practical Demonstration: Calculating the Minimum for ‘game1’


We now apply Method 1 to our sample DataFrame to determine the absolute lowest score recorded in the game1 column. This specific example clearly highlights the elegance and directness of using the agg() function when only one scalar result is needed. By targeting the ‘game1’ column, we instruct the Spark cluster to scan only that column and efficiently identify its lowest integer value across all distributed partitions, leveraging the optimized performance of the underlying min() aggregate function.


The operation df.agg(F.min(‘game1’)) performs the distributed calculation. The subsequent .collect()[0][0] serves as the retrieval mechanism, bringing the single calculated minimum back to the Python environment as a standard integer type. This process is highly optimized, ensuring that even if the DataFrame contained billions of records, the aggregation would be executed efficiently across the cluster nodes before the single result is returned to the driver program.

from pyspark.sql import functions as F

#calculate minimum of column named 'game1'
df.agg(F.min('game1')).collect()[0][0]

10


Upon execution, the system returns the value 10. This confirms that the minimum value encountered in the game1 column is indeed 10. We can manually verify this result by inspecting the data rows for the game1 column, which includes the values 25, 22, 14, 30, 15, and 10. It is evident that 10 represents the lowest score among these observations, validating the accuracy of the PySpark aggregation function and providing immediate insights into the dataset’s lower bounds.

Practical Demonstration: Finding Minimums Across Multiple Columns


Building upon the previous section, we now utilize Method 2 to find the minimum values for all three numerical columns—game1, game2, and game3—in a single, consolidated operation. This is a highly typical requirement in statistical reporting where analysts need a quick, comparative summary of the lower bounds across various metrics simultaneously. By employing the select() transformation, we ensure that the results are maintained within a new, highly structured DataFrame object.


The syntax is both concise and declarative, demonstrating PySpark‘s adherence to functional programming paradigms. We pass three distinct min() functions to the select() method, instructing Spark to calculate the minimum for each column independently but concurrently across the cluster. The resulting output, visualized using .show(), clearly presents these three minimums in a single row, minimizing the need for multiple sequential operations and maximizing readability for rapid data assessment.

from pyspark.sql.functions import min

#calculate minimum for game1, game2 and game3 columns
df.select(min(df.game1), min(df.game2), min(df.game3)).show()

+----------+----------+----------+
|min(game1)|min(game2)|min(game3)|
+----------+----------+----------+
|        10|         8|        10|
+----------+----------+----------+


The summarized output provides three distinct minimum values, confirming the lowest observed performance across each metric:

  • The minimum value in the game1 column is 10.
  • The minimum value in the game2 column is 8.
  • The minimum value in the game3 column is 10.


This demonstration illustrates the power and flexibility of PySpark‘s aggregate functions. By using min(df.column_name), Spark efficiently computes the minimum for each column, handling the distributed computation complexity seamlessly. This method is highly effective for generating summary statistics required for dashboards, initial quality checks, or defining parameters for subsequent filtering operations across massive, large-scale data environments.

Conclusion and Further Resources


Calculating the minimum value of a column in a PySpark DataFrame is a foundational task, but mastering the choice between the available methods is crucial for optimizing workflow efficiency. The decision between using agg() for retrieving single scalar results (requiring collect()) versus select() for multi-column DataFrame outputs hinges on whether you need a Python native value or a new distributed Spark object. Both methods leverage the powerful, optimized aggregate functions within the pyspark.sql.functions module, guaranteeing that the computation scales effectively regardless of the underlying data size.


Mastery of these basic aggregation techniques forms the bedrock of advanced data processing and analytical workloads within the distributed computing environment provided by Apache Spark. The same principles demonstrated here for finding the minimum can be applied directly to calculate other critical statistical measures, including maximums (max()), averages (avg()), standard deviations (stddev()), and counts (count()). These functions similarly benefit from Spark’s distributed architecture, making complex statistical analysis possible on massive datasets that would overwhelm traditional single-machine processing tools. We strongly encourage exploration of the official PySpark documentation for a comprehensive list of available aggregation functions and their specific applications.


The following tutorials explain how to perform other common tasks in PySpark:

Cite this article

Mohammed looti (2025). Learning PySpark: Finding the Minimum Value of a DataFrame Column. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/calculate-the-minimum-value-of-a-column-in-pyspark/

Mohammed looti. "Learning PySpark: Finding the Minimum Value of a DataFrame Column." PSYCHOLOGICAL STATISTICS, 11 Nov. 2025, https://statistics.arabpsychology.com/calculate-the-minimum-value-of-a-column-in-pyspark/.

Mohammed looti. "Learning PySpark: Finding the Minimum Value of a DataFrame Column." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/calculate-the-minimum-value-of-a-column-in-pyspark/.

Mohammed looti (2025) 'Learning PySpark: Finding the Minimum Value of a DataFrame Column', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/calculate-the-minimum-value-of-a-column-in-pyspark/.

[1] Mohammed looti, "Learning PySpark: Finding the Minimum Value of a DataFrame Column," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learning PySpark: Finding the Minimum Value of a DataFrame Column. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top