Но мне не удалось сделать то же самое с ленивым фреймом, хотя эта функция была добавлена в Polars 1.36.0 в 2025/12 году. В частности, я не знаю, как предоставить on_columns для двух столбцов:
# 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...
С помощью DataFrame я могу легко повернуть эту таблицу
id предмет экзамен оценка
1 математика теория 10
1 математика практика 12
3 физика теория 15
В эту таблицу
id {"математика","теория" {"математика","практика" {"физика","теория"
1 10 12
3
15
С помощью этого кода: [code]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 [/code] Но мне не удалось сделать то же самое с ленивым фреймом, хотя эта функция была добавлена в Polars 1.36.0 в 2025/12 году. В частности, я не знаю, как предоставить on_columns для двух столбцов: [code]# 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... [/code] Спасибо за помощь!