nodejs-polars
    Preparing search index...

    Interface GroupBy

    Starts a new GroupBy operation.

    interface GroupBy {
        "[inspect]"(): string;
        agg(...columns: pl.Expr[]): pl.DataFrame;
        agg(
            columns: Record<string, keyof pl.Expr | (keyof pl.Expr)[]>,
        ): pl.DataFrame;
        aggList(): pl.DataFrame;
        first(): pl.DataFrame;
        groups(): pl.DataFrame;
        head(n?: number): pl.DataFrame;
        last(): pl.DataFrame;
        len(): pl.DataFrame;
        max(): pl.DataFrame;
        mean(): pl.DataFrame;
        median(): pl.DataFrame;
        min(): pl.DataFrame;
        nUnique(): pl.DataFrame;
        pivot(pivotCol: string, valuesCol: string): PivotOps;
        pivot(__namedParameters: { pivotCol: string; valuesCol: string }): PivotOps;
        quantile(quantile: number): pl.DataFrame;
        sum(): pl.DataFrame;
        tail(n?: number): pl.DataFrame;
        toString(): string;
    }
    Index

    Methods

    • Compute aggregations for each group of a group by operation.


      Parameters

      • ...columns: pl.Expr[]

        Aggregations to compute for each group of the group by operation, specified as positional arguments.

      Returns pl.DataFrame

      const df = pl.DataFrame({
      foo: [1, 2, 2, 3, 3],
      ham: [6.0, 6, 7, 8.0, 8.0],
      bar: ["a", "b", "c", "c", "c"],
      spam: ["a", "b", "c", "c", "c"],
      });
      // use lazy api rest parameter style
      > df.groupBy('foo', 'bar').agg(pl.count('ham'), pl.col('spam')).sort(["foo", "bar"]);
      shape: (4, 4)
      ┌─────┬─────┬─────┬────────────┐
      foobarhamspam
      │ --- ┆ --- ┆ --- ┆ --- │
      f64stru32list[str] │
      ╞═════╪═════╪═════╪════════════╡
      1.0a1 ┆ ["a"] │
      2.0b1 ┆ ["b"] │
      2.0c1 ┆ ["c"] │
      3.0c2 ┆ ["c", "c"] │
      └─────┴─────┴─────┴────────────┘

      > df.groupBy("bar").agg(pl.col("foo"), pl.col("ham"), pl.col("spam") ).sort("bar");
      shape: (3, 4)
      ┌─────┬─────────────────┬─────────────────┬─────────────────┐
      barfoohamspam
      │ --- ┆ --- ┆ --- ┆ --- │
      strlist[f64] ┆ list[f64] ┆ list[str] │
      ╞═════╪═════════════════╪═════════════════╪═════════════════╡
      a ┆ [1.0] ┆ [6.0] ┆ ["a"] │
      b ┆ [2.0] ┆ [6.0] ┆ ["b"] │
      c ┆ [2.0, 3.0, 3.0] ┆ [7.0, 8.0, 8.0] ┆ ["c", "c", "c"] │
      └─────┴─────────────────┴─────────────────┴─────────────────┘
      > const h = pl.col("ham");
      > df.groupBy("bar").agg(h.sum().as("sum_ham"), h.min().as("min_ham"), h.max().as("max_ham"));
      shape: (3, 4)
      ┌─────┬─────────┬─────────┬─────────┐
      barsum_hammin_hammax_ham
      │ --- ┆ --- ┆ --- ┆ --- │
      strf64f64f64
      ╞═════╪═════════╪═════════╪═════════╡
      a6.06.06.0
      b6.06.06.0
      c23.07.08.0
      └─────┴─────────┴─────────┴─────────┘
    • Parameters

      Returns pl.DataFrame

    • Return first n rows of each group.

      Parameters

      • Optionaln: number

        Number of values of the group to select

      Returns pl.DataFrame

      > df = pl.DataFrame({
      > "letters": ["c", "c", "a", "c", "a", "b"],
      > "nrs": [1, 2, 3, 4, 5, 6]
      > })
      > df
      shape: (6, 2)
      ╭─────────┬─────╮
      lettersnrs
      │ --- ┆ --- │
      stri64
      ╞═════════╪═════╡
      "c"1
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "c"2
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "a"3
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "c"4
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "a"5
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "b"6
      ╰─────────┴─────╯
      > df.groupby("letters")
      > .head(2)
      > .sort("letters");
      > >>
      shape: (5, 2)
      ╭─────────┬─────╮
      lettersnrs
      │ --- ┆ --- │
      stri64
      ╞═════════╪═════╡
      "a"3
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "a"5
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "b"6
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "c"1
      ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
      "c"2
      ╰─────────┴─────╯
    • Return the number of rows in each group.

      Returns pl.DataFrame

      > df = pl.DataFrame({a: ["Apple", "Apple", "Orange"], b: [1, None, 2]});
      > df.groupBy("a").len()
      shape: (2, 2)
      ┌────────┬─────┐
      alen
      │ --- ┆ --- │
      stru32
      ╞════════╪═════╡
      Apple2
      Orange1
      └────────┴─────┘
    • Do a pivot operation based on the group key, a pivot column and an aggregation function on the values column.

      Parameters

      • pivotCol: string

        Column to pivot.

      • valuesCol: string

        Column that will be aggregated.

      Returns PivotOps

    • Parameters

      • __namedParameters: { pivotCol: string; valuesCol: string }

      Returns PivotOps