Each group only has one valid or not_null value in a random row. How do you fill each group with that value?
import polars as pl
data = {
'group': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
'col1': [1, None, None, None, 3, None, None, None, 5],
'col2': ['a', None, None, None, 'b', None, None, None, 'c'],
'col3': [False, None, None, None, True, None, None, None, False]
}
df = pl.DataFrame(data)
shape: (9, 4)
┌───────┬──────┬──────┬───────┐
│ group ┆ col1 ┆ col2 ┆ col3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ bool │
╞═══════╪══════╪══════╪═══════╡
│ 1 ┆ 1 ┆ a ┆ false │
│ 1 ┆ null ┆ null ┆ null │
│ 1 ┆ null ┆ null ┆ null │
│ 2 ┆ null ┆ null ┆ null │
│ 2 ┆ 3 ┆ b ┆ true │
│ 2 ┆ null ┆ null ┆ null │
│ 3 ┆ null ┆ null ┆ null │
│ 3 ┆ null ┆ null ┆ null │
│ 3 ┆ 5 ┆ c ┆ false │
└───────┴──────┴──────┴───────┘
Desired output:
shape: (9, 4)
┌───────┬──────┬──────┬───────┐
│ group ┆ col1 ┆ col2 ┆ col3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str ┆ bool │
╞═══════╪══════╪══════╪═══════╡
│ 1 ┆ 1 ┆ a ┆ false │
│ 1 ┆ 1 ┆ a ┆ false │
│ 1 ┆ 1 ┆ a ┆ false │
│ 2 ┆ 3 ┆ b ┆ true │
│ 2 ┆ 3 ┆ b ┆ true │
│ 2 ┆ 3 ┆ b ┆ true │
│ 3 ┆ 5 ┆ c ┆ false │
│ 3 ┆ 5 ┆ c ┆ false │
│ 3 ┆ 5 ┆ c ┆ false │
└───────┴──────┴──────┴───────┘
In pandas, I can do the following for each column
import pandas as pd
df = pd.DataFrame(data)
df.col1 = df.groupby('group').col.apply(lambda x: x.ffill().bfill())
How do you do this in polars, ideally with a window function (.over()) ?