[Python] How to select the column from a Polars Dataframe that has the largest sum?

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have Polars dataframe with a bunch of columns I need to find the column with, for example, the largest sum.

    The below snippet sums all of the columns:

    df = pl.DataFrame(
    {
    "a": [0, 1, 3, 4],
    "b": [0, 0, 0, 0],
    "c": [1, 0, 1, 0],
    }
    )

    max_col = df.select(pl.col(df.columns).sum())


    shape: (1, 3)
    ┌─────┬─────┬─────┐
    │ a ┆ b ┆ c │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╡
    │ 8 ┆ 0 ┆ 2 │
    └─────┴─────┴─────┘


    But I'm missing the last step of selecting the column with the largest value?

    Continue reading...

Compartilhe esta Página