Built with Tutify.io

    AIStudyLabs

    Polars Counting Elements in List Column

    Polars Counting Elements in List Column

    In Polars, counting elements in a list column refers to the process of determining how many individual items are present within each list inside a column that holds lists of values. This is important when working with data where each row in the column contains multiple items in the form of lists (like a list of numbers, strings, or mixed data types).

    To do this in Polars, you can use the list.len() method, which computes the number of elements in each list. You can also use other functions to count specific occurrences of an item in each list or across the entire list column. In this article, I will explain how to count elements in a list column.

    Key Points –

    <a id="h-usage-of-polars-counting-elements-in-list-column"></a>

    Usage of Polars Counting Elements in List Column

    To count the number of elements in each list within a list column in Polars, you can use the list.len() function. This function computes the length of each list in the column, effectively giving you the count of elements in each list.

    Now, let’s create a Polars DataFrame.

    
    import polars as pl
    
    # Create a DataFrame with a list column
    df = pl.DataFrame({
        "id": [1, 2, 3],
        "values": [[5, 10, 12], [15, 25], [20, 30, 40, 50]]
    })
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To count the total number of elements in each list within the values column, you can use the list.len() method in Polars. This method will return the length of each list in the values column.

    
    # Count the total elements in each list
    df2 = df.with_columns(
        pl.col("values").list.len().alias("num_elements")
    )
    print("DataFrame with Element Counts:\n", df2)
    

    Here,

    <a id="h-count-even-numbers-in-each-list"></a>

    Count Even Numbers in Each List

    To count the even numbers in each list within the 'values' column, you can combine Polars list functions such as list.eval() with a condition to filter even numbers (element % 2 == 0), and then apply list.sum() to get the count.

    
    # Count even numbers in each list
    df2 = df.with_columns(
        pl.col("values")
        .list.eval(pl.element() % 2 == 0)
        .list.sum()
        .alias("even_count")
    )
    print("DataFrame with Even Number Counts:\n", df2)
    
    # Output:
    # DataFrame with Even Number Counts:
    # shape: (3, 3)
    ┌─────┬────────────────┬────────────┐
    │ id  ┆ values         ┆ even_count │
    │ --- ┆ ---            ┆ ---        │
    │ i64 ┆ list[i64]      ┆ u32        │
    ╞═════╪════════════════╪════════════╡
    │ 1   ┆ [5, 10, 12]    ┆ 2          │
    │ 2   ┆ [15, 25]       ┆ 0          │
    │ 3   ┆ [20, 30, … 50] ┆ 4          │
    └─────┴────────────────┴────────────┘
    

    Here,

    <a id="h-count-elements-greater-than-a-value-e-g-gt-25"></a>

    Count Elements Greater than a Value (e.g., > 25)

    To count the elements greater than a specific value (e.g., > 25) in a list column in Polars, you can apply the following method: Use list.eval() to evaluate the condition for each element in the list, and then use list.sum() to count the True values, which correspond to the elements greater than 25.

    
    # Count elements greater than 25
    df2 = df.with_columns(
        pl.col("values")
        .list.eval(pl.element() > 25)  # condition for elements greater than 25
        .list.sum()
        .alias("count_greater_than_25")
    )
    print("DataFrame with Count of Elements Greater Than 25:\n", df2)
    
    # Output:
    # DataFrame with Count of Elements Greater Than 25
    # shape: (3, 3)
    ┌─────┬────────────────┬───────────────────────┐
    │ id  ┆ values         ┆ count_greater_than_25 │
    │ --- ┆ ---            ┆ ---                   │
    │ i64 ┆ list[i64]      ┆ u32                   │
    ╞═════╪════════════════╪═══════════════════════╡
    │ 1   ┆ [5, 10, 12]    ┆ 0                     │
    │ 2   ┆ [15, 25]       ┆ 0                     │
    │ 3   ┆ [20, 30, … 50] ┆ 3                     │
    └─────┴────────────────┴───────────────────────┘
    

    Here,

    <a id="h-count-unique-elements-in-each-list"></a>

    Count Unique Elements in Each List

    To count unique elements in each list within a column in Polars, you can use the list.unique() method to first extract the unique elements from each list and then use list.len() to count the length of the resulting list of unique elements.

    
    import polars as pl
    
    # Create a DataFrame with list column
    df = pl.DataFrame({
        "id": [1, 2, 3],
        "values": [[5, 10, 12, 12], [15, 25, 25], [20, 30, 30, 50]]
    })
    
    # Count unique elements in each list
    df2 = df.with_columns(
        pl.col("values")
        .list.unique()  # Get unique elements in each list
        .list.len()  # Count the unique elements
        .alias("unique_count")
    )
    
    print("DataFrame with Unique Element Counts:\n", df2)
    
    # Output:
    # DataFrame with Unique Element Counts:
    # shape: (3, 3)
    ┌─────┬────────────────┬──────────────┐
    │ id  ┆ values         ┆ unique_count │
    │ --- ┆ ---            ┆ ---          │
    │ i64 ┆ list[i64]      ┆ u32          │
    ╞═════╪════════════════╪══════════════╡
    │ 1   ┆ [5, 10, … 12]  ┆ 3            │
    │ 2   ┆ [15, 25, 25]   ┆ 2            │
    │ 3   ┆ [20, 30, … 50] ┆ 3            │
    └─────┴────────────────┴──────────────┘
    

    Here,

    <a id="h-count-non-null-elements-in-each-list"></a>

    Count Non-null Elements in Each List

    To count the non-null elements in each list within the values column in Polars, you can use list.eval() to identify non-null entries, followed by list.sum() to calculate their count.

    
    import polars as pl
    
    # DataFrame with some null values inside list elements
    df = pl.DataFrame({
        "id": [1, 2, 3],
        "values": [[5, None, 12], [None, None], [20, 30, None, 50]]
    })
    
    # Count non-null elements in each list
    df2 = df.with_columns(
        pl.col("values")
        .list.eval(pl.element().is_not_null())
        .list.sum()
        .alias("non_null_count")
    )
    print("DataFrame with Non-Null Element Counts:\n", df2)
    
    # Output:
    # DataFrame with Non-Null Element Counts:
    # shape: (3, 3)
    ┌─────┬────────────────┬────────────────┐
    │ id  ┆ values         ┆ non_null_count │
    │ --- ┆ ---            ┆ ---            │
    │ i64 ┆ list[i64]      ┆ u32            │
    ╞═════╪════════════════╪════════════════╡
    │ 1   ┆ [5, null, 12]  ┆ 2              │
    │ 2   ┆ [null, null]   ┆ 0              │
    │ 3   ┆ [20, 30, … 50] ┆ 3              │
    └─────┴────────────────┴────────────────┘
    

    Here,

    Similarly, if you want to first remove the nulls, then count the remaining elements directly, you can use list.drop_nulls() followed by list.len() function.

    
    # Count elements after dropping nulls
    df2 = df.with_columns(
        pl.col("values")
        .list.drop_nulls()
        .list.len()
        .alias("count_after_dropping_nulls")
    )
    
    print("DataFrame with Filtered Element Counts:\n", df2)
    
    # Output:
    #  shape: (3, 3)
     shape: (3, 3)
    ┌─────┬────────────────┬────────────────────────────┐
    │ id  ┆ values         ┆ count_after_dropping_nulls │
    │ --- ┆ ---            ┆ ---                        │
    │ i64 ┆ list[i64]      ┆ u32                        │
    ╞═════╪════════════════╪════════════════════════════╡
    │ 1   ┆ [5, null, 12]  ┆ 2                          │
    │ 2   ┆ [null, null]   ┆ 0                          │
    │ 3   ┆ [20, 30, … 50] ┆ 3                          │
    └─────┴────────────────┴────────────────────────────┘
    

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

    Conclusion

    In summary, counting elements in list columns using Polars is powerful, efficient, and expressive. Whether you're counting total elements, filtering based on conditions, handling nulls, or identifying unique values, Polars provides clear, vectorized methods to handle it all seamlessly.

    Happy Learning!!

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

    Related Articles

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

    References