To search over multiple columns, and create a new column of flag if string found, the following codes work, but is there any compact way inside with_columns() to achieve the same?
df = pl.DataFrame({
"col1": ["hello", "world", "polars"],
"col2": ["data", "science", "hello"],
"col3": ["test", "string", "match"],
"col4": ["hello", "example", "test"]
})
search_string = "hello"
condition = pl.lit(False)
for col in df.columns:
condition |= pl.col(col).str.contains(search_string)
df = df.with_columns(
condition.alias("string_found") + 0
)
print(df)
shape: (3, 5)
ββββββββββ¬ββββββββββ¬βββββββββ¬ββββββββββ¬βββββββββββββββ
β col1 β col2 β col3 β col4 β string_found β
β --- β --- β --- β --- β --- β
β str β str β str β str β i32 β
ββββββββββͺββββββββββͺβββββββββͺββββββββββͺβββββββββββββββ‘
β hello β data β test β hello β 1 β
β world β science β string β example β 0 β
β polars β hello β match β test β 1 β
ββββββββββ΄ββββββββββ΄βββββββββ΄ββββββββββ΄βββββββββββββββ
>Solution :
You can use .any_horizontal()
df.with_columns(
pl.any_horizontal(pl.all().str.contains(search_string))
.alias("string_found")
)
shape: (3, 5)
ββββββββββ¬ββββββββββ¬βββββββββ¬ββββββββββ¬βββββββββββββββ
β col1 β col2 β col3 β col4 β string_found β
β --- β --- β --- β --- β --- β
β str β str β str β str β bool β
ββββββββββͺββββββββββͺβββββββββͺββββββββββͺβββββββββββββββ‘
β hello β data β test β hello β true β
β world β science β string β example β false β
β polars β hello β match β test β true β
ββββββββββ΄ββββββββββ΄βββββββββ΄ββββββββββ΄βββββββββββββββ
You can replace pl.all() with pl.col(pl.String) to limit the expression to String columns only.
In this example you only have String columns so it doesn’t come into play.