I have this dataframe:
import polars as pl
df = pl.DataFrame({'value': [1,2,3,4,5,None,None], 'flag': [0,1,1,1,0,0,0]})
┌───────┬──────┐
│ value ┆ flag │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═══════╪══════╡
│ 1 ┆ 0 │
│ 2 ┆ 1 │
│ 3 ┆ 1 │
│ 4 ┆ 1 │
│ 5 ┆ 0 │
│ null ┆ 0 │
│ null ┆ 0 │
└───────┴──────┘
I want to use df.with_columns(pl.col('value').forward_fill()) (or similar), but I only want to use values that have flag == 1 to be eligible for filling. So in this example, I want value 4 to be used to replace the two null entries (rather than 5).
┌───────┬──────┐
│ value ┆ flag │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═══════╪══════╡
│ 1 ┆ 0 │
│ 2 ┆ 1 │
│ 3 ┆ 1 │
│ 4 ┆ 1 │
│ 5 ┆ 0 │
│ 4 ┆ 0 │
│ 4 ┆ 0 │
└───────┴──────┘
How can one achieve this?