1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Polars: What is the fastest way to loop over Polars DataFrame columns to apply...

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 21:12.

  1. Stack

    Stack Membro Participativo

    Is there a preferred way to loop and apply functions to Polars columns?

    Here is a pandas example of what I am trying to do:

    df1 = pl.DataFrame(
    {
    "A": np.random.rand(10),
    "B": np.random.rand(10),
    "C": np.random.rand(10)
    }
    )
    df2 = pl.DataFrame(
    {
    "X1": np.random.rand(10),
    "X2": np.random.rand(10),
    "X3": np.random.rand(10)
    }
    )


    # pandas code

    # this is just a weighted sum of df2, where the weights are from df1
    df1.to_pandas().apply(
    lambda weights: df2.to_pandas().mul(weights, axis=0).sum() / weights.sum(), axis=0,
    result_type='expand'
    )


    A B C
    X1 0.647355 0.705358 0.692214
    X2 0.500439 0.416325 0.384294
    X3 0.601890 0.606301 0.577076

    Continue reading...

Compartilhe esta Página