[Python] polars: multiply 2 LazyFrames together by column

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I have 2 polars LazyFrames:

    df1 = pl.DataFrame(data={
    'foo': np.random.uniform(0,127, size= n).astype(np.float64),
    'bar': np.random.uniform(1e3,32767, size= n).astype(np.float64),
    'baz': np.random.uniform(1e6,2147483, size= n).astype(np.float64)
    }).lazy()

    df2 = pl.DataFrame(data={
    'foo': np.random.uniform(0,127, size= n).astype(np.float64),
    'bar': np.random.uniform(1e3,32767, size= n).astype(np.float64),
    'baz': np.random.uniform(1e6,2147483, size= n).astype(np.float64)
    }).lazy()


    I would like to multiply each column in df1 with its respective column in df2.

    If I convert these to non-lazy DataFrames I can achieve this:

    df1.collect() * df2.collect()


    foo bar baz
    f64 f64 f64
    3831.295563 6.4637e6 3.3669e12
    164.194271 2.9691e8 2.2696e12
    3655.918761 1.9444e7 2.3625e12
    7191.48868 3.7044e7 3.1687e12
    9559.505277 2.6864e8 2.5426e12


    However, if I try to perform the same expression on the LazyFrames, I get an exception

    df1 * df2


    TypeError: unsupported operand type(s) for *: 'LazyFrame' and 'LazyFrame'
    ​

    How can I perform column-wise multiplication across 2 LazyFrames?

    Continue reading...

Compartilhe esta Página