1

I'd like to replace any value greater than some condition with zero for any column except the date column in a df. The closest I've found it

df.with_columns( 
   pl.when(pl.any_horizontal(pl.col(pl.Float32) > 20))
   .then(0)
   .otherwise(pl.col(pl.Float32))
)

But this will zero out the entire row.

My not working code is -

df=df.select(
    pl.col("date"),
    pl.when(pl.col(pl.Float32) > 20).then(0))

In Pandas I would do something like

df.where(df>2, 0)
2
  • if you get error then show it in question (not in comments) as text (not image) Commented Nov 5 at 0:37
  • maybe better create minimal working code with small example data directly in code (and with expected output) - so we could simply copy and test it. Commented Nov 5 at 0:37

2 Answers 2

2

I found the answer -

test = test.with_columns(
    pl.when(pl.col(pl.Float32) > 8)
      .then(0)
      .otherwise(pl.col(pl.Float32)).name.keep()
)

I needed otherwise and to explicitly keep the column names. I thought the expression expansion was the issue but this works.

Sign up to request clarification or add additional context in comments.

3 Comments

Technically, you do not answer your own question. You wanted to "replace any value... for any column except the date column", but instead replaced it for only float32 columns
but their answer would work with pl.all(). I think the important bit is they're not having to list out the column names.
I guess the unsaid part of my question is every other column is a float except the date column so this works for my purposes. As Dean said, I didn't want to list every column
2
df = pl.DataFrame({
    "date": [date(2025, 1, 1), date(2026, 1, 1)],
    "a": [1, 3],
    "b": [4.5, 1]
})

cols_excl_date = pl.exclude("date")

df.with_columns(pl.when(cols_excl_date <= 2).then(cols_excl_date).otherwise(0))

# or this works too

df.with_columns(pl.when(cols_excl_date > 2).then(0).otherwise(cols_excl_date).name.keep())

1 Comment

Great to know you can do col expression expansion outside the select statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.