[Python] Change column type in Polars DataFrame

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

  1. Stack

    Stack Membro Participativo

    I have a Polars DataFrame below.

    import polars as pl
    df = pl.DataFrame({"a":["1.2", "2.3", "5.4"],
    "b":["0.4", "0.03", "0.12"],
    "c":["AA", "BB", "CC"]})

    >>> df
    a b c
    str str str
    -------------------------
    "1.2" "0.04" "AA"
    "2.3" "0.3" "BB"
    "3.5" "0.12" "CC"


    How can I convert the columns to specific types? In this case, I want to convert columns a and b into floats.

    I expect below.

    >>> df
    a b c
    f64 f64 str
    ------------------------
    1.2 0.04 "AA"
    2.3 0.3 "BB"
    3.5 0.12 "CC"

    Continue reading...

Compartilhe esta Página