# Chalk DataFrames Reference: Window and Aggregation
source: https://docs.chalk.ai/dataframe-docs#section-window-and-aggregation

Methods for computing group summaries and window (analytic) expressions.

- `group_by` — returns a [`GroupBy`](https://docs.chalk.ai/dataframe-docs#GroupBy) object for chained aggregations
- `agg` — group by columns and apply aggregation expressions directly
- `window` — compute analytic (window) expressions partitioned by key columns

## `group_by(by)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.group_by

Create a GroupBy object for chained aggregation operations.

This method returns a GroupBy object that can be used to apply aggregation expressions via the `.agg()` method. This provides an alternative syntax to `df.agg(by, *aggregations)`.

**Returns**

`GroupBy`

**Examples**

```python
from chalkdf import DataFrame
from chalk.features import _
df = DataFrame.from_dict({"group": ["A", "A", "B"], "value": [1, 2, 3]})
grouped = df.group_by("group").agg(_.value.sum().alias("total"))
```

Multiple grouping columns:

```python
df2 = DataFrame.from_dict({"g1": ["A", "A", "B"], "g2": ["X", "Y", "X"], "val": [1, 2, 3]})
result = df2.group_by("g1", "g2").agg(_.val.sum().alias("sum"))
```

Using underscore expressions:

```python
result = df.group_by(_.group).agg(_.value.mean().alias("avg"))
```

## `agg(by, aggregations, pre_grouped_keys)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.agg

Group by columns and apply aggregation expressions.

**Parameters**

- `by`: `Sequence[str | Underscore] | str | Underscore`
  Column name(s) to group by. Can be a single column name/expression or a sequence of column names/expressions.
- `aggregations`: `AggExpr | Underscore` = `()`
- `pre_grouped_keys`: `Sequence[str]` = `()`

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
from chalk.features import _
df = DataFrame.from_dict({"group": ["A", "A", "B"], "value": [1, 2, 3]})
agg_df = df.agg(["group"], _.value.sum().alias("total"))
# Or with a single column:
agg_df = df.agg("group", _.value.sum().alias("total"))
```

## `window(by, order_by, expressions)`
source: https://docs.chalk.ai/dataframe-docs#DataFrame.window

Compute window (analytic) expressions partitioned by `by` and ordered by `order_by`.

Window operations evaluate each `WindowExpr` over a _partition_ of rows (defined by `by`) sorted within that partition (by `order_by`). The result columns are appended to the existing schema; original columns are preserved.

Overlap between `by` and `order_by` columns is not allowed.

**Parameters**

- `by`: `Sequence[str | Underscore]`
  Column names that define the partition boundaries. Rows with the same combination of values in these columns form one partition.
- `order_by`: `Sequence[str | Underscore | tuple[str | Underscore, str] | tuple[str | Underscore, str, str]]`
  Column names (or `(name, direction)` / `(name, direction, nulls_order)` tuples) that define the sort order _within_ each partition. Direction can be `"asc"` (default) or `"desc"`. `nulls_order` can be `"nulls_first"`/`"first"` or `"nulls_last"`/`"last"` (defaults to nulls last).
- `expressions`: `WindowExpr` = `()`

**Returns**

`DataFrame`

**Examples**

```python
from chalkdf import DataFrame
from libchalk.chalktable import WindowExpr
df = DataFrame.from_dict({
    "idx": [1, 1, 2, 2],
    "v":   [10, 20, 30, 40],
})
# Partition by "idx", sort by "v" ascending, shift "v" by -1 into "v_shifted"
result = df.window(["idx"], ["v"], WindowExpr.shift("v", "v_shifted", -1))
# result schema: idx, v, v_shifted
# v_shifted contains the *next* value of v within each idx partition
```
