List-size feature engineering
Add a derived column containing the size of each row's list-valued field.
Polars · Module
Counts the number of elements in every list represented by an expression. Null elements are included, and the result is a UInt32 expression.
Apply .list.len() to a list-valued expression, such as pl.col("a"), to produce an expression that counts the items in every row's list. The result has UInt32 type, and an item whose value is null still contributes to the count.
Expr.list.len() → ExprExpr of data type UInt32
with_columns.import polars as pl
df = pl.DataFrame({"a": [[1, 2, None], [5], []]})
result = df.with_columns(length=pl.col("a").list.len())
print(result)Adds a UInt32 column containing the number of items in each list, including null items.
Add a derived column containing the size of each row's list-valued field.
Filter or inspect rows based on whether their nested lists are empty or have a target size.
Summarize nested data before exploding or otherwise reshaping list columns.
Expr.list.len() return?Expr.list.len() returns an expression that counts the number of elements in each list. Its result has UInt32 data type, and null elements are included in that count.
Follow-up: How would you apply it to a DataFrame column? Use df.with_columns(pl.col("a").list.len()) or give the resulting expression an output name.
Use a list-valued column expression, such as pl.col("a").list.len(), inside a DataFrame expression context. Each output value is the length of the corresponding row's list.
Follow-up: How does an empty list behave? It contributes zero elements, so its resulting length is 0.
The operation counts list positions rather than only non-null values. Therefore, a list containing two integers and one null produces a length of three.
Follow-up: How would you count only non-null items? First transform or filter the list values to remove nulls, then apply a length operation.
Expr.listExpr.list.__getitem__Expr.list.allExpr.list.eval