polars.Expr.cat.starts_with#
- Expr.cat.starts_with(prefix: str) Expr [source]#
Check if string representations of values start with a substring.
- Parameters:
- prefix
Prefix substring.
See also
contains
Check if string repr contains a substring that matches a pattern.
ends_with
Check if string repr end with a substring.
Notes
Whereas
str.starts_with
allows expression inputs,cat.starts_with
requires a literal string value.Examples
>>> df = pl.DataFrame( ... {"fruits": pl.Series(["apple", "mango", None], dtype=pl.Categorical)} ... ) >>> df.with_columns( ... pl.col("fruits").cat.starts_with("app").alias("has_prefix"), ... ) shape: (3, 2) ┌────────┬────────────┐ │ fruits ┆ has_prefix │ │ --- ┆ --- │ │ cat ┆ bool │ ╞════════╪════════════╡ │ apple ┆ true │ │ mango ┆ false │ │ null ┆ null │ └────────┴────────────┘
Using
starts_with
as a filter condition:>>> df.filter(pl.col("fruits").cat.starts_with("app")) shape: (1, 1) ┌────────┐ │ fruits │ │ --- │ │ cat │ ╞════════╡ │ apple │ └────────┘