# xarray.Dataset.stack

#### Dataset.stack(dim=None, create_index=True, index_cls=<class 'xarray.core.indexes.PandasMultiIndex'>, \*\*dim_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L5370-L5419)

Stack any number of existing dimensions into a single new dimension.

New dimensions will be added at the end, and by default the corresponding
coordinate variables will be combined into a MultiIndex.

* **Parameters:**
  * **dim** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to [sequence](https://docs.python.org/3/glossary.html#term-sequence) of [hashable](../user-guide/terminology.html.md#term-name)) – Mapping of the form new_name=(dim1, dim2, …). Names of new
    dimensions, and the existing dimensions that they replace. An
    ellipsis (…) will be replaced by all unlisted dimensions.
    Passing a list containing an ellipsis (stacked_dim=[…]) will stack over
    all dimensions.
  * **create_index** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) –
    - True: create a multi-index for each of the stacked dimensions.
    - False: don’t create any index.
    - None. create a multi-index only if exactly one single (1-d) coordinate
      index is found for every dimension to stack.
  * **index_cls** (`Index-class`, *default*: `PandasMultiIndex`) – Can be used to pass a custom multi-index type (must be an Xarray index that
    implements .stack()). By default, a pandas multi-index wrapper is used.
  * **\*\*dim_kwargs** – The keyword arguments form of `dim`.
    One of dim or dim_kwargs must be provided.
* **Returns:**
  **stacked** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Dataset with stacked data.

#### SEE ALSO
[`Dataset.unstack`](xarray.Dataset.unstack.html.md#xarray.Dataset.unstack)
