Built with Tutify.io

    AIStudyLabs

    Polars DataFrame median() Usage & Examples

    Polars DataFrame median() Usage & Examples

    In Polars, the median() function is used to compute the median (the middle value) of numerical columns in a DataFrame. It automatically ignores non-numeric columns and null values. The median is the middle value of a sorted dataset, making it a useful measure of central tendency, especially when the data contains outliers.

    In this article, I will explain the Polars DataFrame median() function and by using its syntax, parameters, and usage how we can generate a new DataFrame with a single row of median values, excluding non-numeric columns.

    Key Points –

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

    Polars DataFrame median() Introduction

    Let’s know the syntax of the polars DataFrame.median() function.

    
    # Syntax of median()
    polars.median(*columns: str) → Expr
    

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

    Parameters of the Polars DataFrame.median()

    It allows only one parameter.

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

    Return Value

    This function returns a new DataFrame with a single row containing median values. Non-numeric columns are ignored.

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

    Usage of Polars DataFrame median() Function

    The median() function calculates the middle value of each numeric column in a Polars DataFrame. If a column has an odd number of values, it returns the middle value; if even, it returns the average of the two middle values.

    Now, let’s create a Polars DataFrame and learn how to use the median with an example.

    
    import polars as pl
    
    df = pl.DataFrame({
        'A': [12, 24, 46, 18, 30],
        'B': [15, 42, 26, 55, 89],
        'C': [67, 16, 71, 29, 53]
    })
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can calculate the median of all numeric columns in a Polars DataFrame using the median() method. This function automatically selects only numerical columns and calculates their median (middle) values.

    
    # Compute median for all numeric columns
    df2 = df.median()
    print("Median of each numeric column:\n", df2)
    

    Here,

    <a id="h-computing-median-for-specific-columns-using-pl-median"></a>

    Computing Median for Specific Columns Using pl.median()

    You can compute the median for specific columns in a Polars DataFrame using the pl.col().median() function.

    
    # Compute median for column "A"
    df2 = df.select(pl.median("A"))
    print("Median of column A:\n", df2)
    
    # Output:
    # Median of column A:
    # shape: (1, 1)
    ┌──────┐
    │ A    │
    │ ---  │
    │ f64  │
    ╞══════╡
    │ 24.0 │
    └──────┘
    

    Here,

    <a id="h-computing-median-for-multiple-columns"></a>

    Computing Median for Multiple Columns

    You can compute the median for multiple columns in a Polars DataFrame using pl.col().median() inside the select() method.

    
    # Compute median for multiple columns
    df2 = df.select(pl.col(["A", "B"]).median())
    print("Median of columns A and B:\n", df2)
    
    # Compute median for columns "A" and "B"
    df2 = df.select(pl.median("A", "B"))
    print("Median of columns A and B:\n", df2)
    
    # Output:
    # Median of columns A and B:
    # shape: (1, 2)
    ┌──────┬──────┐
    │ A    ┆ B    │
    │ ---  ┆ ---  │
    │ f64  ┆ f64  │
    ╞══════╪══════╡
    │ 24.0 ┆ 42.0 │
    └──────┴──────┘
    

    Here,

    <a id="h-compute-median-for-a-filtered-dataframe"></a>

    Compute Median for a Filtered DataFrame

    You can compute the median after filtering specific rows based on a condition using filter() before applying pl.median(). This is useful when you want to compute the median for a subset of your data.

    
    # Compute median for the filtered DataFrame
    filtered_df = df.filter(pl.col("A") > 30)
    df2 = filtered_df.select(pl.all().median())
    print("Median values for filtered DataFrame:\n", df2)
    
    # Filter rows where column "A" is greater than 30 and compute median
    df2 = df.filter(df["A"] > 30).select(pl.median("A", "B", "C"))
    print("Median values for filtered DataFrame:\n", df2)
    
    # Output:
    # Median values for filtered DataFrame:
    # shape: (1, 3)
    ┌──────┬──────┬──────┐
    │ A    ┆ B    ┆ C    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ 46.0 ┆ 26.0 ┆ 71.0 │
    └──────┴──────┴──────┘
    

    Here,

    <a id="h-handling-missing-values-none-or-nan"></a>

    Handling Missing Values (None or NaN)

    When handling missing values (None or NaN), Polars' median() function automatically ignores null values by default. However, you can explicitly handle missing values in different ways.

    
    import polars as pl
    
    df = pl.DataFrame({
        'A': [12, None, 46, 18, 30],
        'B': [None, 42, 26, 55, 89],
        'C': [67, 16, None, 29, 53]
    })
    
    # Handling missing values
    df2 = df.median()
    print("Median ignoring missing values:\n", df2)
    
    # Output:
    # Median ignoring missing values:
    # shape: (1, 3)
    ┌──────┬──────┬──────┐
    │ A    ┆ B    ┆ C    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ 24.0 ┆ 48.5 ┆ 41.0 │
    └──────┴──────┴──────┘
    

    Here,

    If you want to fill missing values before computing the median using the fill_null() method. This ensures that missing (None or NaN) values do not affect the computation.

    
    # Fill missing values before computing median
    df2 = df.fill_null(0).median()
    print("Median after filling missing values with 0:\n", df2)
    
    # Output:
    # Median after filling missing values with 0:
    # shape: (1, 3)
    ┌──────┬──────┬──────┐
    │ A    ┆ B    ┆ C    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ 18.0 ┆ 42.0 ┆ 29.0 │
    └──────┴──────┴──────┘
    

    Here,

    <a id="h-compute-median-with-negative-numbers"></a>

    Compute Median with Negative Numbers

    When computing the median in Polars with negative numbers, the process remains the same as with positive numbers. The median is simply the middle value when the numbers are sorted.

    
    import polars as pl
    
    df = pl.DataFrame({
        'A': [-12, -24, 46, 18, 30],
        'B': [15, -42, 26, -55, 89],
        'C': [67, 16, -71, 29, -53]
    })
    
    df2 = df.median()
    print("Median with negative numbers:\n", df2)
    
    # Output:
    # Median with negative numbers:
    # shape: (1, 3)
    ┌──────┬──────┬──────┐
    │ A    ┆ B    ┆ C    │
    │ ---  ┆ ---  ┆ ---  │
    │ f64  ┆ f64  ┆ f64  │
    ╞══════╪══════╪══════╡
    │ 18.0 ┆ 15.0 ┆ 16.0 │
    └──────┴──────┴──────┘
    

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

    Conclusion

    In summary, the median() function in Polars is a powerful tool for computing the middle value of numeric columns in a DataFrame. It is particularly useful for analyzing datasets that include both positive and negative numbers while efficiently handling missing values (None/NaN).

    Happy Learning!!

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

    Related Articles

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

    References