[Python] Why won't polars.when and then run in jupyters notebook?

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    im trying to insert/make a new column with these attributes in lines of then("") but i get columnNotFoundError instead for the lines of .then(). but these does not exists yet, what is wrong with the code?

    df_2023 = df_2023.with_columns(
    pl.when(pl.col("Tillatt totalvekt opp til og med 3500").eq("X"))
    .then("Opp til og med 3500")
    .when(pl.col("Tillatt totalvekt 3501-7500").eq("X"))
    .then("3501-7500")
    .when(pl.col("Tillatt totalvekt over 7500").eq("X"))
    .then("Over 7500")
    .otherwise(pl.lit(None).cast(pl.String))
    .alias("Tillatt totalvekt")
    )

    df_2023.select("Tillat totalvekt").head()


    *THE ERROR*

    *ColumnNotFoundError: Opp til og med 3500*

    I also tried with:
    pl.when(pl.col("Tillatt totalvekt opp til og med 3500") == "X")
    .then("Opp til og med 3500")

    I could really use some help, read also the Polars documentation for polars.when, but it was for no help to solve this problem.

    Thanks in advance!

    Continue reading...

Compartilhe esta Página