Question
Polars apply function to check if a row value is a substring of another string
I'm trying to check if string_1 = "this example string"
contains a column value as a substring.
For example the first value in Col B
should be True
since "example"
is a substring of string_1
string_1 = "this example string"
df = pl.from_repr("""
┌────────┬─────────┬────────────┐
│ Col A ┆ Col B ┆ Col C │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞════════╪═════════╪════════════╡
│ 448220 ┆ example ┆ 7101936801 │
│ 518398 ┆ 99999 ┆ 9999900091 │
│ 557232 ┆ 424570 ┆ 4245742060 │
└────────┴─────────┴────────────┘
""")
This is what I have tried so far, but it's returning the following error:
df=df.with_columns(pl.col("Col B").apply(lambda x: x in string_1).alias("new_col"))
AttributeError: 'Expr' object has no attribute 'apply'
3 81
3