id
предмет
экзамен
оценка
1
математика
теория
10
1
математика
практика
12
3
физика
теория
15
В эту таблицу
id
{"математика","теория"
{"математика","практика"
{"физика","теория"
1
10
12
3
15
С помощью этого кода:
Код: Выделить всё
import polars as pl
df = pl.DataFrame(
{
"id": [1, 1, 3],
"subject": ["maths", "maths", "physics"],
"exam": ["theory", "practice", "theory"],
"grade": [10, 12, 15],
}
)
lf = df.lazy()
df.pivot(on=["subject", "exam"], index="id") # works well
Код: Выделить всё
# A list of sets
lf.pivot(
on=["subject", "exam"],
index="id",
on_columns=[{"maths", "theory"}, {"maths", "practice"}, {"physics", "theory"}],
)
# InvalidOperationError: `pivot` expected `on` and `on_columns` to have the same amount of columns.
# A list of lists
lf.pivot(
on=["subject", "exam"],
index="id",
on_columns=[["maths", "theory"], ["maths", "practice"], ["physics", "theory"]],
)
# InvalidOperationError...
# A series
lf.pivot(
on=["subject", "exam"],
index="id",
on_columns=lf.select(pl.struct("subject", "exam").unique()).collect().to_series(),
)
# InvalidOperationError...
Подробнее здесь: https://stackoverflow.com/questions/798 ... le-columns