Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars getting ColumnNotFound when use the when() method

I'm trying to use a combination of contains method together when in polars. However, I'm getting the following annoying error:

Exception has occurred: ColumnNotFoundError
Foo message.

I read the documentation, but the examples are quite simple and don't cover a situation like this. Here follows my code sample:

df = df.with_columns(
    pl.when(pl.col("Foo").str.contains("Foo message. Bar Message."))
    .then("Foo message")
    .alias("Foo_Column")
)

Any help is appreciated.

like image 260
Guilherme Noronha Avatar asked Aug 02 '26 03:08

Guilherme Noronha


1 Answers

You're probably looking for

df = df.with_columns(
    pl.when(pl.col("Foo").str.contains("Foo message. Bar Message."))
    .then(pl.lit("Foo message"))
    .alias("Foo_Column")
)

otherwise Polars thinks "Foo message" is the name of a column

like image 188
ignoring_gravity Avatar answered Aug 04 '26 17:08

ignoring_gravity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!