Вдохновленный этим постом, с которого я начал:
Код: Выделить всё
df = pl.DataFrame({"a": [2, 0, 1, 0, 0, 0], "b": [1, 1, 1, 0, 0, 1]})
crosstab = (
df.pivot(on="b", index="a", values="b", aggregate_function="len", sort_columns=True)
.fill_null(0)
.sort("a")
)
crosstab
Код: Выделить всё
def perc_cols(df):
tot = df.select(~cs.by_index(0)).to_numpy().sum()
for col in df.columns[1:]:
yield ((pl.col(col) / tot) * 100)
crosstab.select(cs.by_index(0), perc_cols(crosstab))
Код: Выделить всё
TypeError: cannot create expression literal for value of type generator.
Подробнее здесь: https://stackoverflow.com/questions/797 ... -in-polars
Мобильная версия