# xarray.DataArray.unstack

#### DataArray.unstack(dim=None, \*, fill_value=<NA>, sparse=False)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L3118-L3190)

Unstack existing dimensions corresponding to MultiIndexes.

The dimensions are unstacked into multiple new dimensions.

New dimensions will be added at the end.

* **Parameters:**
  * **dim** ([`str`](xarray.DataArray.str.html.md#xarray.DataArray.str), `Iterable` of `Hashable` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dimension(s) over which to unstack. By default unstacks all
    MultiIndexes.
  * **fill_value** ([scalar](../user-guide/terminology.html.md#term-scalar) or [dict-like](https://docs.python.org/3/glossary.html#term-mapping), *default*: `nan`) – Value to be filled. If a dict-like, maps variable names to
    fill values. Use the data array’s name to refer to its
    name. If not provided or if the dict-like does not contain
    all variables, the dtype’s NA value will be used.
  * **sparse** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – Use sparse-array if True
* **Returns:**
  **unstacked** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array with unstacked data.

#### SEE ALSO
[`DataArray.stack`](xarray.DataArray.stack.html.md#xarray.DataArray.stack)

### Notes

The order of the new dimension coordinates follows the levels of the
`MultiIndex`. A `MultiIndex` created by pandas (e.g. when converting
a `pd.Series` with a `MultiIndex`) usually has sorted levels, so the
resulting coordinates will be sorted. A `MultiIndex` created by
[`DataArray.stack()`](xarray.DataArray.stack.html.md#xarray.DataArray.stack) preserves the original order. Use
[`DataArray.sel()`](xarray.DataArray.sel.html.md#xarray.DataArray.sel) after unstacking to select a specific order if
needed.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f04d0ecd-fffc-4502-9869-6c145bd42a65','6128e2e0-91bc-4fcb-aa73-a456829983cd','dc93702c-b0b1-4b32-b696-92b130df453d','../lite/tree/../notebooks/index.html?path=07105bf2_a89d_412f_bcdd_ab0a097289d5.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> arr = xr.DataArray(
...     np.arange(6).reshape(2, 3),
...     coords=[("x", ["a", "b"]), ("y", [0, 1, 2])],
... )
>>> arr
<xarray.DataArray (x: 2, y: 3)> Size: 48B
array([[0, 1, 2],
       [3, 4, 5]])
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) int64 24B 0 1 2
>>> stacked = arr.stack(z=("x", "y"))
>>> stacked.indexes["z"]
MultiIndex([('a', 0),
            ('a', 1),
            ('a', 2),
            ('b', 0),
            ('b', 1),
            ('b', 2)],
           name='z')
>>> roundtripped = stacked.unstack()
>>> arr.identical(roundtripped)
True
```

<div id="dc93702c-b0b1-4b32-b696-92b130df453d" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f04d0ecd-fffc-4502-9869-6c145bd42a65','dc93702c-b0b1-4b32-b696-92b130df453d')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f04d0ecd-fffc-4502-9869-6c145bd42a65','dc93702c-b0b1-4b32-b696-92b130df453d')">Open In Tab</button></div><div id="6128e2e0-91bc-4fcb-aa73-a456829983cd" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
