0

I have a dataset where null/missing values are represented by 0. So I'd like to do something like c.replace_val(0, "forward"). What is a good/easy/efficient way to do that? Thanks.

JulioBarros
  • 43
  • 1
  • 5

1 Answers1

1
  1. if you are reading from a file you can specify your null_values and then use a .forward_fill() in one pass:

    d = pl.read_csv('file.csv', null_values=0)

    d.a.fill_null('forward')

  1. if your are not reading from a file, I am afraid you need to impute your 0s first and then replace them. You can chain when/then/otherwise/fill_null. E.g.,

    d.with_column( pl.when(col("a") == 0) .then(None) .otherwise(col("a")) .fill_null("forward")

Pasqui
  • 591
  • 4
  • 12