Built with Tutify.io

    AIStudyLabs

    Polars DataFrame min() Function

    Polars DataFrame min() Function

    In Polars, the min() function is used to compute the minimum value of a column or across multiple columns in a DataFrame. It is similar to the min() function in pandas but optimized for speed and efficiency in Polars. By default, this function ignores missing values (null or NaN), ensuring that the computation excludes them when determining the minimum value.

    In this article, I will explain the Polars DataFrame min() function, covering its syntax, parameters, and usage, as well as how to return a new DataFrame with a single row containing the minimum values from each column.

    Key Points –

    <a id="h-polars-dataframe-min-introduction"></a>

    Polars DataFrame min() Introduction

    Following is a syntax of the DataFrame.min(). This function takes *names params.

    
    # Syntax of min()
    polars.min(*names: str) → Expr
    

    <a id="h-parameters-of-the-polars-dataframe-min"></a>

    Parameters of the Polars DataFrame.min()

    It allows only one parameter.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new DataFrame containing a single row with the minimum values for each column.

    <a id="h-usage-of-polars-dataframe-min-function"></a>

    Usage of Polars DataFrame.min() Function

    The DataFrame.min() method in Polars is used to compute the minimum value for each column in a DataFrame. It returns a new DataFrame with a single row containing the minimum values of each respective column. This function is especially useful when you want to quickly identify the lowest values across multiple columns, without needing to manually filter or compute.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Creating a sample DataFrame
    data = {
        'A': [5, 14, 9, 18, 25],
        'B': [52, 16, 36, 15, 42],
        'C': [14, 11, 23, 59, 84]
    }
    
    df = pl.DataFrame(data)
    print("Original DataFrame:\n",df)
    

    Yields below output.

    To find the minimum value of each column in the DataFrame, you can use the min() method. When applied to a DataFrame without any parameters, this function returns the minimum values for each column.

    
    # Find the minimum value of each column
    df2 = df.min()
    print("Minimum Value of Each Column:\n", df2)
    

    In the above example, calculate the minimum value for each column and return a new DataFrame with the results.

    <a id="h-find-the-minimum-value-of-a-specific-column"></a>

    Find the Minimum Value of a Specific Column

    To find the minimum value of a specific column in a Polars DataFrame, you can use the min() function in combination with the select() method to specify the column.

    
    # Finding the minimum value of column 'A'
    df2 = df.select(pl.col("A").min())
    print("Minimum value of column 'A':\n", df2)
    
    # Finding the minimum value of column 'A'
    df2 = df.select(pl.min("A"))
    print("Minimum value of column 'A':\n", df2)
    
    # Output:
    # Minimum value of column 'A':
    # shape: (1, 1)
    ┌─────┐
    │ A   │
    │ --- │
    │ i64 │
    ╞═════╡
    │ 5   │
    └─────┘
    

    In the above example, this code will return a DataFrame containing the minimum value from column 'A'. You can replace "A" with the name of any other column to get its minimum value.

    <a id="h-find-the-minimum-across-specific-columns"></a>

    Find the Minimum Across Specific Columns

    To find the minimum values across specific columns in a Polars DataFrame, you can use the select() method and apply the min() function to the selected columns.

    
    # Finding the minimum values across columns 'A' and 'B'
    df2 = df.select(pl.min("A", "B"))
    print("Minimum values across columns 'A' and 'B':\n", df2)
    
    # Output:
    # Minimum values across columns 'A' and 'B':
    # shape: (1, 2)
    ┌─────┬─────┐
    │ A   ┆ B   │
    │ --- ┆ --- │
    │ i64 ┆ i64 │
    ╞═════╪═════╡
    │ 5   ┆ 15  │
    └─────┴─────┘
    

    In the above example, compute the minimum value for each of the specified columns ('A' and 'B'). You can replace "A" and "B" with any columns you want to analyze.

    <a id="h-find-the-minimum-value-in-a-dataframe-with-negative-values"></a>

    Find the Minimum Value in a DataFrame with Negative Values

    To find the minimum value in a DataFrame with negative values, you can use the min() function as you would with any other DataFrame. Polars will properly handle negative values and include them in the calculation of the minimum.

    
    import polars as pl
    
    # Creating a sample DataFrame with negative values
    data = {
        'A': [-5, -14, 9, -18, 25],
        'B': [52, -16, -36, 15, -42],
        'C': [14, -11, 23, -59, 84]
    }
    
    df = pl.DataFrame(data)
    
    # Finding the minimum value for each column
    df2 = df.min()
    print("Minimum values for each column:\n", df2)
    
    # Output:
    # Minimum values for each column:
    # shape: (1, 3)
    ┌─────┬─────┬─────┐
    │ A   ┆ B   ┆ C   │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ -18 ┆ -42 ┆ -59 │
    └─────┴─────┴─────┘
    

    In the above example, the min() function will accurately determine the minimum values for each column, even when negative numbers are involved. For column A, the minimum value is -18, for column B it is -42, and for column C it is -59.

    <a id="h-using-min-on-columns-with-missing-values"></a>

    Using min() on Columns with Missing Values

    When using the min() function on columns with missing values (i.e., null or NaN), Polars will ignore those missing values by default and compute the minimum based only on the available non-missing values.

    
    import polars as pl
    
    # Creating a sample DataFrame with missing values
    data = {
        'A': [5, None, 9, 18, 25],  
        'B': [52, 16, None, 15, 42],  
        'C': [14, 11, 23, None, 84]  
    }
    
    df = pl.DataFrame(data)
    
    # Finding the minimum value for each column, ignoring missing values
    df2 = df.min()
    print("Minimum values for each column (ignoring missing values):\n", df2)
    
    # Output:
    # Minimum values for each column (ignoring missing values):
    # shape: (1, 3)
    ┌─────┬─────┬─────┐
    │ A   ┆ B   ┆ C   │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ 5   ┆ 15  ┆ 11  │
    └─────┴─────┴─────┘
    

    In the above example, missing values (None or null) are ignored when calculating the minimum of each column. Column A has a minimum of 5, column B has 15 (ignoring the null), and column C has 11 (ignoring the null).

    If you want to replace nulls before computing the min, you can use fill_null(0).

    
    # Replace nulls with a default value (e.g., 0) before finding min
    df2 = df.fill_null(0).select(pl.all().min())
    print("Minimum values after filling nulls with 0:\n", df2)
    
    # Output:
    # Minimum values after filling nulls with 0:
    # shape: (1, 3)
    ┌─────┬─────┬─────┐
    │ A   ┆ B   ┆ C   │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ 0   ┆ 0   ┆ 0   │
    └─────┴─────┴─────┘
    

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, the min() function in Polars is a powerful tool for computing the minimum values in specific columns of a DataFrame. It streamlines the process of identifying minimum values while enhancing data analysis and manipulation in Polars.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References