Built with Tutify.io

    AIStudyLabs

    Polars Series filter() Method with Examples

    Polars Series filter() Method with Examples

    In Polars, the Series.filter() function is used to retrieve specific values from a Series based on a Boolean condition (also known as a mask). It returns a new Series containing only the elements where the condition is True.

    In this article, I will explain the Polars Series filter() method by using its syntax, parameters, and usage to demonstrate how it returns a new Series that contains only the values where the Boolean condition is True.

    Key Points –

    <a id="h-polars-series-filter-introduction"></a>

    Polars Series filter() Introduction

    Let’s know the syntax of the series filter() function.

    
    # Syntax of polars Series filter()
    Series.filter(predicate: Series | Iterable[bool]) → Self
    

    <a id="h-parameters-of-the-polars-series-filter"></a>

    Parameters of the Polars Series filter()

    The following are the parameters of the polars Series filter() method.

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

    Return Value

    This function returns a new Series, containing only the elements where the corresponding predicate value is True.

    <a id="h-usage-of-polars-series-filter-function"></a>

    Usage of Polars Series filter() Function

    The Series.filter() method is used to select specific values from a Polars Series based on a Boolean condition (or Boolean mask). It returns a new Series containing only the elements where the mask is True.

    Now, let’s create a Polars Series.

    
    import polars as pl
    
    ser = pl.Series("numbers", [10, 15, 20, 25, 30])
    print("Original Series:\n", ser)
    

    Yields below output.

    To filter values greater than a number in a Polars Series, you can use the filter() function along with a Boolean condition.

    
    # Filter values greater than 20
    result = ser.filter(ser > 20)
    print("Filtered Series (values > 20):\n", result)
    

    Here,

    <a id="h-filter-even-numbers"></a>

    Filter Even Numbers

    To filter even numbers from a Polars Series, you can use the modulo operator % to check if a number is divisible by 2 (i.e., number % 2 == 0).

    
    # Filter even numbers
    result = ser.filter(ser % 2 == 0)
    print("Even Numbers:\n", result)
    
    # Output:
    # Even Numbers:
    # shape: (3,)
    # Series: 'numbers' [i64]
    # [
    #	10
    #	20
    #	30
    # ]
    

    Here,

    <a id="h-filter-values-less-than-or-equal-to-15"></a>

    Filter Values Less Than or Equal to 15

    To filter values less than or equal to 15 in a Polars Series, you can use the <= operator to create a boolean mask, and then use filter() to get the matching values.

    
    # Filter values <= 15
    result = ser.filter(ser <= 15)
    print("Filtered Series (values <= 15):\n", result)
    
    # Output:
    # Filtered Series (values <= 15):
    # shape: (2,)
    # Series: 'numbers' [i64]
    # [
    # 	10
    # 	15
    # ]
    

    Here,

    <a id="h-filter-values-that-are-multiples-of-5-explain"></a>

    Filter Values That are Multiples of 5 explain

    To filter values that are multiples of 5 in a Polars Series, you can use the modulo operator (%) to check if the remainder when dividing by 5 is zero.

    
    # Filter values that are multiples of 5
    result = ser.filter(ser % 5 == 0)
    print("Multiples of 5:\n", result)
    
    # Output:
    # Multiples of 5:
    # shape: (5,)
    # Series: 'numbers' [i64]
    # [
    # 	10
    # 	15
    # 	20
    # 	25
    # 	30
    # ]
    

    Here,

    <a id="h-filter-using-a-manual-boolean-mask-python-list"></a>

    Filter Using a Manual Boolean Mask (Python List)

    You can manually filter a Polars Series using a Boolean mask provided as a plain Python list of True/False values.

    
    # Define a manual Boolean mask (must be the same length as the Series)
    mask = [True, False, True, False, True]
    
    # Apply the filter
    result = ser.filter(mask)
    print("Filtered Series (manual mask):\n", result)
    
    # Output:
    # Filtered Series (manual mask):
    # shape: (3,)
    # Series: 'numbers' [i64]
    # [
    # 	10
    # 	20
    # 	30
    # ]
    

    Here,

    <a id="h-filter-using-a-condition-with-logical-and"></a>

    Filter Using a Condition with Logical AND

    Filter a Polars Series using the logical AND operator (&). This means selecting values that meet all specified conditions simultaneously.

    
    # Filter values greater than 10 AND less than 30
    result = ser.filter((ser > 10) & (ser < 30))
    print("Filtered Series (10 < values < 30):\n", result)
    
    # Output:
    # Filtered Series (10 < values < 30):
    # shape: (3,)
    # Series: 'numbers' [i64]
    # [
    #	15
    #	20
    #	25
    # ]
    

    Here,

    <a id="h-filter-using-a-condition-with-logical-or"></a>

    Filter Using a Condition with Logical OR

    Filter a Polars Series using a condition with the logical OR operator (|). This allows you to select values that meet at least one of multiple conditions.

    
    # Filter values less than 15 OR greater than 25
    result= ser.filter((ser < 15) | (ser > 25))
    print("Filtered Series (values < 15 or > 25):\n", result)
    
    # Output:
    # Filtered Series (values < 15 or > 25):
    # shape: (2,)
    # Series: 'numbers' [i64]
    # [
    # 	10
    # 	30
    # ]
    

    Here,

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

    Conclusion

    In conclusion, the Series.filter() method in Polars is a powerful and efficient way to extract specific values based on conditions. By using Boolean masks, you can easily select elements that meet specific criteria, making your data processing tasks both efficient and expressive.

    Happy Learning!!

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

    Related Articles

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

    Reference