[Python] In Polars, how can I figure out which column each value returned by .min_horizontal()...

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    In python Polars, df.min_horizontal() returns a pl.Series with the minimum value of each row in the DataFrame.

    How can I figure out which column each value in the Series originally came from?

    For example, for:

    df = pl.DataFrame({
    "a": [1, 8, 3],
    "b": [4, 5, 2],
    "c": [7, 2, 9]
    })
    min_values = df.min_horizontal()


    min_values will be a pl.Series [1,2,2].

    I'd like to also get a series or column ['a', 'c', 'b'], which are the names of the columns the minimum values came from.

    I can't come up with a solution that does not involve iterating row-wise or colum-wise over the whole DataFrame, but I feel like there must be a more elegant way.

    Continue reading...

Compartilhe esta Página