Найдите последнее (крайнее справа) ненулевое значение в каждой строке.Python

Программы на Python
Anonymous
Найдите последнее (крайнее справа) ненулевое значение в каждой строке.

Сообщение Anonymous »

У меня есть следующий кадр данных Polars

Код: Выделить всё

import polars as pl

data = {
"product_id": ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
"col1": ["a", "a", "a", "a", "a", "a", "a", "a", "a",],
"col2": ["b", None, "b", None, "b", None, "b", None, "b"],
"col3": ["c", None, "c", None, None, None, None, None, None],
"col4": [None, None, None, None, None, "d", None, None, "d"]
}

df = pl.DataFrame(data)

Код: Выделить всё

┌────────────┬──────┬──────┬──────┬──────┐
│ product_id ┆ col1 ┆ col2 ┆ col3 ┆ col4 │
│ ---        ┆ ---  ┆ ---  ┆ ---  ┆ ---  │
│ str        ┆ str  ┆ str  ┆ str  ┆ str  │
╞════════════╪══════╪══════╪══════╪══════╡
│ 1          ┆ a    ┆ b    ┆ c    ┆ null │
│ 2          ┆ a    ┆ null ┆ null ┆ null │
│ 3          ┆ a    ┆ b    ┆ c    ┆ null │
│ 4          ┆ a    ┆ null ┆ null ┆ null │
│ 5          ┆ a    ┆ b    ┆ null ┆ null │
│ 6          ┆ a    ┆ null ┆ null ┆ d    │
│ 7          ┆ a    ┆ b    ┆ null ┆ null │
│ 8          ┆ a    ┆ null ┆ null ┆ null │
│ 9          ┆ a    ┆ b    ┆ null ┆ d    │
└────────────┴──────┴──────┴──────┴──────┘

Мне нужно заполнить нулевые значения в столбце col4 ближайшим ненулевым значением из столбцов слева (не учитывая столбец product_id) и создать новый столбец на основе этих значений.
Желаемый результат должен быть следующим:

Код: Выделить всё

data = {
"product_id": ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
"col1": ["a", "a", "a", "a", "a", "a", "a", "a", "a",],
"col2": ["b", None, "b", None, "b", None, "b", None, "b"],
"col3": ["c", None, "c", None, None, None, None, None, None],
"col4": [None, None, None, None, None, "d", None, None, "d"],
"desired_column": ["c", "a", "c", "a", "b", "d", "b", "a", "d"]
}

df = pl.DataFrame(data)

Код: Выделить всё

shape: (9, 6)
┌────────────┬──────┬──────┬──────┬──────┬────────────────┐
│ product_id ┆ col1 ┆ col2 ┆ col3 ┆ col4 ┆ desired_column │
│ ---        ┆ ---  ┆ ---  ┆ ---  ┆ ---  ┆ ---            │
│ str        ┆ str  ┆ str  ┆ str  ┆ str  ┆ str            │
╞════════════╪══════╪══════╪══════╪══════╪════════════════╡
│ 1          ┆ a    ┆ b    ┆ c    ┆ null ┆ c              │
│ 2          ┆ a    ┆ null ┆ null ┆ null ┆ a              │
│ 3          ┆ a    ┆ b    ┆ c    ┆ null ┆ c              │
│ 4          ┆ a    ┆ null ┆ null ┆ null ┆ a              │
│ 5          ┆ a    ┆ b    ┆ null ┆ null ┆ b              │
│ 6          ┆ a    ┆ null ┆ null ┆ d    ┆ d              │
│ 7          ┆ a    ┆ b    ┆ null ┆ null ┆ b              │
│ 8          ┆ a    ┆ null ┆ null ┆ null ┆ a              │
│ 9          ┆ a    ┆ b    ┆ null ┆ d    ┆ d              │
└────────────┴──────┴──────┴──────┴──────┴────────────────┘
Я пробовал разные подходы, но безуспешно. Есть ли способ добиться такого поведения в полярах?

Подробнее здесь: https://stackoverflow.com/questions/774 ... n-each-row

Вернуться в «Python»