[Python] Multiple aggregations on multiple columns in Python polars

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

  1. Stack

    Stack Membro Participativo

    Checking out how to implement binning with Python polars, I can easily calculate aggregates for individual columns:

    import polars as pl
    import numpy as np

    t, v = np.arange(0, 100, 2), np.arange(0, 100, 2)
    df = pl.DataFrame({"t": t, "v0": v, "v1": v})
    df = df.with_columns((pl.datetime(2022,10,30) + pl.duration(seconds=df["t"])).alias("datetime")).drop("t")

    df.group_by_dynamic("datetime", every="10s").agg(pl.col("v0").mean())


    shape: (10, 2)
    ┌─────────────────────┬──────┐
    │ datetime ┆ v0 │
    │ --- ┆ --- │
    │ datetime[μs] ┆ f64 │
    ╞═════════════════════╪══════╡
    │ 2022-10-30 00:00:00 ┆ 4.0 │
    │ 2022-10-30 00:00:10 ┆ 14.0 │
    │ 2022-10-30 00:00:20 ┆ 24.0 │
    │ 2022-10-30 00:00:30 ┆ 34.0 │
    │ ... ┆ ... │


    or calculate multiple aggregations like

    df.group_by_dynamic("datetime", every="10s").agg(
    pl.col("v0").mean().alias("v0_binmean"),
    pl.col("v0").count().alias("v0_bincount")
    )
    ┌─────────────────────┬────────────┬─────────────┐
    │ datetime ┆ v0_binmean ┆ v0_bincount │
    │ --- ┆ --- ┆ --- │
    │ datetime[μs] ┆ f64 ┆ u32 │
    ╞═════════════════════╪════════════╪═════════════╡
    │ 2022-10-30 00:00:00 ┆ 4.0 ┆ 5 │
    │ 2022-10-30 00:00:10 ┆ 14.0 ┆ 5 │
    │ 2022-10-30 00:00:20 ┆ 24.0 ┆ 5 │
    │ 2022-10-30 00:00:30 ┆ 34.0 ┆ 5 │
    │ ... ┆ ... ┆ ... │


    or calculate one aggregation for multiple columns like

    cols = [c for c in df.columns if "datetime" not in c]
    df.group_by_dynamic("datetime", every="10s").agg(
    pl.col(f"{c}").mean().alias(f"{c}_binmean")
    for c in cols
    )

    ┌─────────────────────┬────────────┬────────────┐
    │ datetime ┆ v0_binmean ┆ v1_binmean │
    │ --- ┆ --- ┆ --- │
    │ datetime[μs] ┆ f64 ┆ f64 │
    ╞═════════════════════╪════════════╪════════════╡
    │ 2022-10-30 00:00:00 ┆ 4.0 ┆ 4.0 │
    │ 2022-10-30 00:00:10 ┆ 14.0 ┆ 14.0 │
    │ 2022-10-30 00:00:20 ┆ 24.0 ┆ 24.0 │
    │ 2022-10-30 00:00:30 ┆ 34.0 ┆ 34.0 │
    │ ... ┆ ... ┆ ... │


    However, combining both approaches fails!

    df.group_by_dynamic("datetime", every="10s").agg(
    [
    pl.col(f"{c}").mean().alias(f"{c}_binmean"),
    pl.col(f"{c}").count().alias(f"{c}_bincount")
    ]
    for c in cols
    )


    DuplicateError: column with name 'literal' has more than one occurrences


    Is there a "polarustic" approach to calculate multiple statistical parameters for multiple (all) columns of the dataframe in one go?

    related, pandas-specific: Python pandas groupby aggregate on multiple columns

    Continue reading...

Compartilhe esta Página