Built with Tutify.io

    AIStudyLabs

    Polars Filter by Column Value

    Polars Filter by Column Value

    In Polars, you can filter a DataFrame by column value using the filter() method, which operates similarly to Pandas but is optimized for speed and efficiency. Filtering can be performed using filter(), is_in(), str.contains(), and is_null() functions. Additionally, you can apply logical operators like | (OR), & (AND), and == (equal) for more advanced filtering. In this article, I will explain how to filter a Polars DataFrame by column values.

    Key Points –

    <a id="h-usage-of-polars-filter-by-column-value"></a>

    Usage of Polars Filter by Column Value

    The filter() method in Polars is used to select specific rows from a DataFrame based on column values. It is highly efficient and supports multiple conditions for filtering data.

    To run some examples of polars filter by column value, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Creating a new Polars DataFrame
    technologies= {
        'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
        'Fees' :[22000,25000,20000,24000,26000],
        'Duration':['30days','50days','40days','50days','40days'],
        'Discount':[1000,2300,1500,1200,2500]
    }
    
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To filter rows in a Polars DataFrame where a column equals a specific value, you can use the filter() method.

    
    # Filtering rows where 'Courses' is 'Python'
    df2 = df.filter(df["Courses"] == "Python")
    print("Filtered DataFrame:\n", df2)
    

    Here,

    <a id="h-filter-rows-where-a-column-is-greater-than-a-value"></a>

    Filter Rows Where a Column is Greater than a Value

    To filter rows in a Polars DataFrame where a column is greater than a specific value, use the filter() method with a comparison operator (>).

    
    # Filtering rows where 'Fees' is greater than 22000
    df2 = df.filter(df["Fees"] > 22000)
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     │
    │ Python  ┆ 24000 ┆ 50days   ┆ 1200     │
    │ Pandas  ┆ 26000 ┆ 40days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-filter-rows-where-a-column-is-within-a-list-of-values-is-in"></a>

    Filter Rows Where a Column is Within a List of Values (is_in)

    To filter rows in a Polars DataFrame where a column's values are within a specific list, use the is_in() method inside filter() function.

    
    # Filtering rows where 'Courses' is in ["Python", "Spark"]
    df2= df.filter(df["Courses"].is_in(["Python", "Spark"]))
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     │
    │ Python  ┆ 24000 ┆ 50days   ┆ 1200     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-filter-rows-where-a-column-is-not-equal-to-a-value"></a>

    Filter Rows Where a Column is Not Equal to a Value

    To filter rows where a column is not equal to a specific value in Polars, use the != operator or the filter() method.

    
    # Filter rows where Duration is not '50days'
    df2 = df.filter(df["Duration"] != "50days")
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     │
    │ Hadoop  ┆ 20000 ┆ 40days   ┆ 1500     │
    │ Pandas  ┆ 26000 ┆ 40days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-filter-rows-using-multiple-conditions-and-amp"></a>

    Filter Rows Using Multiple Conditions (AND &)

    To filter rows in Polars using multiple conditions (logical AND &), you can use the filter() method with multiple conditions inside parentheses.

    
    # Filtering rows where 'Fees' > 22000 AND 'Discount' < 2000
    df2= df.filter((df["Fees"] > 22000) & (df["Discount"] < 2000))
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (1, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Python  ┆ 24000 ┆ 50days   ┆ 1200     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-filter-rows-where-a-string-column-contains-a-substring"></a>

    Filter Rows Where a String Column Contains a Substring

    To filter rows in a Polars DataFrame where a string column contains a specific substring, use the str.contains() function inside filter() function.

    
    # Filtering rows where 'Courses' contains "Spark"
    df2 = df.filter(df["Courses"].str.contains("Spark"))
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     │
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-filter-rows-where-a-column-value-is-missing-null"></a>

    Filter Rows Where a Column Value is Missing (null)

    You can filter rows where a column value is missing (null) using the is_null() method inside filter() function.

    
    import polars as pl
    
    # Creating a Polars DataFrame with some null values
    technologies = {
        'Courses': ["Spark", "PySpark", "Hadoop", "Python", "Pandas"],
        'Fees': [22000, 25000, 20000, None, 26000],  
        'Duration': ['30days', '50days', None, '50days', '40days'],  
        'Discount': [1000, 2300, 1500, None, 2500] 
    }
    
    df = pl.DataFrame(technologies)
    
    # Filtering rows where 'Discount' is null
    df2 = df.filter(df["Discount"].is_null())
    print("Filtered DataFrame:\n", df2)
    
    # Output:
    # Filtered DataFrame:
    # shape: (1, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    │ Python  ┆ null ┆ 50days   ┆ null     │
    └─────────┴──────┴──────────┴──────────┘
    

    Here,

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

    Conclusion

    In summary, filtering by column values in Polars is a highly efficient and powerful method for extracting relevant data from a DataFrame. The filter() method enables users to apply various conditions, including equality, inequality, numerical comparisons, list-based filtering, and logical operations. By utilizing functions like filter(), is_in(), and str.contains(), you can efficiently process large datasets with minimal performance impact.

    Happy Learning!!

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

    Related Articles

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

    Reference