[Python] How to create a rank on two columns in python polars?

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    Suppose we have this dataframe in polars (python):

    import polars as pl
    df = pl.DataFrame(
    {
    "era": ["01", "01", "02", "02", "03", "03"],
    "pred": [3,5,6,8,9,1]
    }
    )


    I can create a rank/row_number based on one column, like:

    df.with_columns(rn = pl.col("era").rank("ordinal"))


    But if I want to do it based on two columns, it is not working:

    df.with_columns(rn = pl.col(["era","pred"]).rank("ordinal"))


    I get this error message:

    ComputeError: The name: 'rn' passed to `LazyFrame.with_columns` is duplicate
    Error originated just after this operation:
    DF ["era", "pred"]; PROJECT */2 COLUMNS; SELECTION: "None"


    Any suggestions on how to do this?

    Continue reading...

Compartilhe esta Página