# xarray.Dataset.groupby

#### Dataset.groupby(group=None, , squeeze=False, restore_coord_dims=False, eagerly_compute_group=None, \*\*groupers)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L10322-L10433)

Returns a DatasetGroupBy object for performing grouped operations.

* **Parameters:**
  * **group** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`IndexVariable`](xarray.IndexVariable.html.md#xarray.IndexVariable) or [sequence](https://docs.python.org/3/glossary.html#term-sequence) of [hashable](../user-guide/terminology.html.md#term-name) or [mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [`Grouper`](xarray.groupers.Grouper.html.md#xarray.groupers.Grouper)) – Array whose unique values should be used to group this array. If a
    Hashable, must be the name of a coordinate contained in this dataarray. If a dictionary,
    must map an existing variable name to a `Grouper` instance.
  * **squeeze** ([`False`](https://docs.python.org/3/builtins/constants.html#False)) – This argument is deprecated.
  * **restore_coord_dims** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, also restore the dimension order of multi-dimensional
    coordinates.
  * **eagerly_compute_group** ([`False`](https://docs.python.org/3/builtins/constants.html#False), *optional*) – This argument is deprecated.
  * **\*\*groupers** (`Mapping` of [`str`](https://docs.python.org/3/builtins/stdtypes.html#str) to [`Grouper`](xarray.groupers.Grouper.html.md#xarray.groupers.Grouper) or [`Resampler`](xarray.groupers.Resampler.html.md#xarray.groupers.Resampler)) – Mapping of variable name to group by to `Grouper` or `Resampler` object.
    One of `group` or `groupers` must be provided.
    Only a single `grouper` is allowed at present.
* **Returns:**
  **grouped** ([`DatasetGroupBy`](xarray.core.groupby.DatasetGroupBy.html.md#xarray.core.groupby.DatasetGroupBy)) – A DatasetGroupBy object patterned after pandas.GroupBy that can be
  iterated over in the form of (unique_value, grouped_array) pairs.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('b6f80296-a075-4f22-9a53-41e6a48baff6','339a5f83-bae2-4e06-80ff-fd639a5ac91e','44edc93a-7e90-4025-946f-c0f38efb42db','../lite/tree/../notebooks/index.html?path=dc64d044_8e36_433e_8b98_d28877389246.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> ds = xr.Dataset(
...     {"foo": (("x", "y"), np.arange(12).reshape((4, 3)))},
...     coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))},
... )
```

Grouping by a single variable is easy

```pycon
>>> ds.groupby("letters")
<DatasetGroupBy, grouped over 1 grouper(s), 2 groups in total:
    'letters': UniqueGrouper('letters'), 2/2 groups with labels 'a', 'b'>
```

Execute a reduction

```pycon
>>> ds.groupby("letters").sum()
<xarray.Dataset> Size: 64B
Dimensions:  (letters: 2, y: 3)
Coordinates:
  * letters  (letters) object 16B 'a' 'b'
Dimensions without coordinates: y
Data variables:
    foo      (letters, y) int64 48B 9 11 13 9 11 13
```

Grouping by multiple variables

```pycon
>>> ds.groupby(["letters", "x"])
<DatasetGroupBy, grouped over 2 grouper(s), 8 groups in total:
    'letters': UniqueGrouper('letters'), 2/2 groups with labels 'a', 'b'
    'x': UniqueGrouper('x'), 4/4 groups with labels 10, 20, 30, 40>
```

Use Grouper objects to express more complicated GroupBy operations

```pycon
>>> from xarray.groupers import BinGrouper, UniqueGrouper
>>>
>>> ds.groupby(x=BinGrouper(bins=[5, 15, 25]), letters=UniqueGrouper()).sum()
<xarray.Dataset> Size: 144B
Dimensions:  (y: 3, x_bins: 2, letters: 2)
Coordinates:
  * x_bins   (x_bins) interval[int64, right] 32B (5, 15] (15, 25]
  * letters  (letters) object 16B 'a' 'b'
Dimensions without coordinates: y
Data variables:
    foo      (y, x_bins, letters) float64 96B 0.0 nan nan 3.0 ... nan nan 5.0
```

<div id="44edc93a-7e90-4025-946f-c0f38efb42db" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('b6f80296-a075-4f22-9a53-41e6a48baff6','44edc93a-7e90-4025-946f-c0f38efb42db')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('b6f80296-a075-4f22-9a53-41e6a48baff6','44edc93a-7e90-4025-946f-c0f38efb42db')">Open In Tab</button></div><div id="339a5f83-bae2-4e06-80ff-fd639a5ac91e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[GroupBy: Group and Bin Data](../user-guide/groupby.html.md#groupby)
: Users guide explanation of how to group and bin data.

[Computational Patterns](https://tutorial.xarray.dev/intermediate/computation/01-high-level-computation-patterns.html)
: Tutorial on `Groupby()` for windowed computation.

[Grouped Computations](https://tutorial.xarray.dev/fundamentals/03.2_groupby_with_xarray.html)
: Tutorial on `Groupby()` demonstrating reductions, transformation and comparison with [`resample()`](xarray.Dataset.resample.html.md#xarray.Dataset.resample).

[`pandas.DataFrame.groupby`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html#pandas.DataFrame.groupby)
[`Dataset.groupby_bins`](xarray.Dataset.groupby_bins.html.md#xarray.Dataset.groupby_bins)
[`DataArray.groupby`](xarray.DataArray.groupby.html.md#xarray.DataArray.groupby)
[`core.groupby.DatasetGroupBy`](xarray.core.groupby.DatasetGroupBy.html.md#xarray.core.groupby.DatasetGroupBy)
[`Dataset.coarsen`](xarray.Dataset.coarsen.html.md#xarray.Dataset.coarsen)
[`Dataset.resample`](xarray.Dataset.resample.html.md#xarray.Dataset.resample)
[`DataArray.resample`](xarray.DataArray.resample.html.md#xarray.DataArray.resample)
