Built with Tutify.io

    AIStudyLabs

    Polars DataFrame fill_null() Usage & Examples

    Polars DataFrame fill_null() Usage & Examples

    In Polars, the fill_null() method is used to replace None (null) values in a DataFrame or Series with a specified value or strategy. Properly handling missing values leads to cleaner and more reliable data for analysis. This function is similar to fillna() in pandas.

    In this article, I will discuss the fill_null() function in Polars DataFrame, covering its syntax, parameters, and usage. I will also demonstrate how to replace None values in a DataFrame using different filling strategies.

    Key Points –

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

    Polars DataFrame fill_null() Introduction

    Let’s know the syntax of the fill_null() method.

    
    # Syntax of fill_null()
    DataFrame.fill_null(
    value: Any | Expr | None = None,
    strategy: FillNullStrategy | None = None,
    limit: int | None = None,
    *,
    matches_supertype: bool = True,
    ) → DataFrame
    

    <a id="h-parameters-of-the-dataframe-fill-null"></a>

    Parameters of the DataFrame fill_null()

    Following are the parameters of the fill_null() method.

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

    Return Value

    This function returns a DataFrame where None values are replaced based on the specified filling strategy.

    <a id="h-usage-of-polars-dataframe-fill-null-method"></a>

    Usage of Polars DataFrame fill_null() Method

    The fill_null() method in Polars is used to replace null (missing) values in a DataFrame or Series with a specified value or a computed strategy.

    First, let’s create a Polars DataFrame.

    
    import polars as pl
    
    df = pl.DataFrame(({
         'Courses':["Spark",'Java',"Scala",'Python'],
         'Fee' :[20000,None,26000,24000],
         'Duration':['30days','40days', None,'40days'],
         'Discount':[1000,None,2500,None]
                   }))
    print("Original DataFrame:\n", df)
    

    Yields below output.

    You can use the fill_null() method in Polars to replace null (missing) values with a fixed value.

    
    # Replace nulls with a fixed value
    df2 = df.fill_null(99)
    print("DataFrame after replacing nulls:\n", df2)
    

    Here,

    You can also replace nulls in specific columns using the fill_null() method on individual columns.

    
    # Replace nulls in the 'Fee' column with 0 and in 'Discount' column with 500
    df2 = df.with_columns(
        df["Fee"].fill_null(0),
        df["Discount"].fill_null(500)
    )
    print("DataFrame after replacing nulls in specific columns:\n", df2)
    
    # Output:
    # DataFrame after replacing nulls in specific columns:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 0     ┆ 40days   ┆ 500      │
    │ Scala   ┆ 26000 ┆ null     ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 500      │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-forward-fill-forward-propagate-previous-values"></a>

    Forward Fill ("forward") - Propagate Previous Values

    You can use the fill_null(strategy="forward") method in Polars to fill None values by propagating the last non-null value down the column, also known as forward filling.

    
    # Forward fill missing values
    df2 = df.fill_null(strategy="forward")
    print("DataFrame after forward fill:\n", df2)
    
    # Output:
    # DataFrame after forward fill:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 20000 ┆ 40days   ┆ 1000     │
    │ Scala   ┆ 26000 ┆ 40days   ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-backward-fill-backward-use-next-available-value"></a>

    Backward Fill ("backward") - Use Next Available Value

    You can use the fill_null(strategy="backward") method in Polars to replace None values with the next available value in the same column (also known as backward filling).

    
    # Backward fill missing values
    df2 = df.fill_null(strategy="backward")
    print("DataFrame after backward fill:\n", df2)
    
    # Output:
    # DataFrame after backward fill:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 26000 ┆ 40days   ┆ 2500     │
    │ Scala   ┆ 26000 ┆ 40days   ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ null     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-replace-null-values-using-min-or-max-strategy"></a>

    Replace Null Values Using "min" or "max" Strategy

    You can use the fill_null(strategy="min") or fill_null(strategy="max") method in Polars to replace None values with the minimum or maximum value of the respective column. Alternatively, the fill_null() method allows you to handle null values by applying the "min" or "max" strategy in a Polars DataFrame.

    
    # Replace null values with column-wise minimum values
    df2 = df.fill_null(strategy="min")
    print("DataFrame after filling nulls with min values:\n", df2)
    
    # Output:
    # DataFrame after filling nulls with min values:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 20000 ┆ 40days   ┆ 1000     │
    │ Scala   ┆ 26000 ┆ 30days   ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 1000     │
    └─────────┴───────┴──────────┴──────────┘
    
    # Replace null values with column-wise maximum values
    df2 = df.fill_null(strategy="max")
    print("\nDataFrame after filling nulls with max values:\n", df2)
    
    # Output:
    # DataFrame after filling nulls with max values:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 26000 ┆ 40days   ┆ 2500     │
    │ Scala   ┆ 26000 ┆ 40days   ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-column-wise-replacement-using-a-dictionary"></a>

    Column-Wise Replacement Using a Dictionary

    You can use the fill_null() method with a dictionary to replace None values with specific values per column in Polars.

    
    # Define column-wise replacement values
    fill_values = {
        "Fee": 15000,        # Replace nulls in 'Fee' with 15000
        "Duration": "Unknown", # Replace nulls in 'Duration' with 'Unknown'
        "Discount": 700      # Replace nulls in 'Discount' with 700
    }
    
    # Replace null values using a dictionary
    df_filled = df.with_columns([
        df[col].fill_null(value) for col, value in fill_values.items()
    ])
    print("DataFrame after column-wise filling:\n", df_filled)
    
    # Output:
    # DataFrame after column-wise filling:
    # shape: (4, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 20000 ┆ 30days   ┆ 1000     │
    │ Java    ┆ 15000 ┆ 40days   ┆ 700      │
    │ Scala   ┆ 26000 ┆ Unknown  ┆ 2500     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 700      │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

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

    Conclusion

    In summary, the fill_null() method in Polars is a versatile solution for managing missing values in a DataFrame. It enables you to replace nulls with a fixed value, propagate values using forward or backward fill, or apply strategies such as "min", "max", or "mean".

    Happy Learning!!

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

    Related Articles

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

    References