I would like to create a cross table that shows, in each cell, the percentages of rows over the total number of rows. Inspired by this post I started with: 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 and then inspired by polars' user guide I tried to convert values into percentages with: 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)) but I get an error: TypeError: cannot create expression literal for value of type generator. notice that both crosstab.select(cs.by_index(0)) and crosstab.select(perc_cols(crosstab)) works as expected. Continue reading...