Search code examples
pythonpandasdataframepython-polars

Add row of column totals in polars


I'm trying to move away from pandas to polars, I have a use case where I need to add the column totals as a new row to the polars lazy frame.

I've found this answer and it would perfectly solve my problem in pandas, but I haven't found any way or documentation on how to translate this code to polars.


Solution

  • To create a new row with the total of each column, you can first create a row of the column totals using an aggregation and then concatenate it to the dataframe using pl.concat.

    pl.concat([df, df.select(pl.all().sum())])
    

    Previous answer

    Disclaimer. My original answer was based on the answer to the linked pandas question. It actually create a new column with the total of each row.

    To create a new column total that is given by the horizontal sum of all other columns, use pl.sum_horizontal as follows.

    df.with_columns(
        pl.sum_horizontal(pl.all()).alias("total")
    )
    

    If you don't want to sum all columns, give the corresponding column names directly to pl.sum_horizontal, e.g. pl.sum_horizontal("A", "B", "C").