Anonymous
Полярные значения: заполняйте нули единственным допустимым значением в каждой группе.
Сообщение
Anonymous » 07 дек 2024, 14:19
Каждая группа имеет только одно допустимое или ненулевое значение в случайной строке. Как заполнить каждую группу этим значением?
Код: Выделить всё
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 │
└───────┴──────┴──────┴───────┘
Желаемый результат:
Код: Выделить всё
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 │
└───────┴──────┴──────┴───────┘
В pandas я могу сделать следующее для каждого столбца
Код: Выделить всё
import pandas as pd
df = pd.DataFrame(data)
df.col1 = df.groupby('group').col.apply(lambda x: x.ffill().bfill())
Как это сделать в полярах, в идеале с помощью оконной функции (.over())?
Подробнее здесь:
https://stackoverflow.com/questions/758 ... each-group
1733570348
Anonymous
Каждая группа имеет только одно допустимое или ненулевое значение в случайной строке. Как заполнить каждую группу этим значением? [code]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) [/code] [code]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 │ └───────┴──────┴──────┴───────┘ [/code] Желаемый результат: [code]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 │ └───────┴──────┴──────┴───────┘ [/code] В pandas я могу сделать следующее для каждого столбца [code]import pandas as pd df = pd.DataFrame(data) df.col1 = df.groupby('group').col.apply(lambda x: x.ffill().bfill()) [/code] Как это сделать в полярах, в идеале с помощью оконной функции (.over())? Подробнее здесь: [url]https://stackoverflow.com/questions/75818269/polars-fill-nulls-with-the-only-valid-value-within-each-group[/url]