# xarray.Dataset.set_index

#### Dataset.set_index(indexes=None, append=False, \*\*indexes_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L4816-L4979)

Set Dataset (multi-)indexes using one or more existing coordinates
or variables.

This legacy method is limited to pandas (multi-)indexes and
1-dimensional “dimension” coordinates. See
[`set_xindex()`](xarray.Dataset.set_xindex.html.md#xarray.Dataset.set_xindex) for setting a pandas or a custom
Xarray-compatible index from one or more arbitrary coordinates.

* **Parameters:**
  * **indexes** (`{dim: index, ...}`) – Mapping from names matching dimensions and values given
    by (lists of) the names of existing coordinates or variables to set
    as new (multi-)index.
  * **append** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, append the supplied index(es) to the existing index(es).
    Otherwise replace the existing index(es) (default).
  * **\*\*indexes_kwargs** (**optional**) – The keyword arguments form of `indexes`.
    One of indexes or indexes_kwargs must be provided.
* **Returns:**
  **obj** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Another dataset, with this dataset’s data but replaced coordinates.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f82fa6b3-6260-4151-bc8a-4bbd51b0e75e','50cfb93f-d4f9-47f4-aac7-c347be879beb','6b3765ad-6ae8-4e98-8d30-09536a2cad5a','../lite/tree/../notebooks/index.html?path=8dd3f85c_292e_4a89_93f3_14fa4bd6340c.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> arr = xr.DataArray(
...     data=np.ones((2, 3)),
...     dims=["x", "y"],
...     coords={"x": range(2), "y": range(3), "a": ("x", [3, 4])},
... )
>>> ds = xr.Dataset({"v": arr})
>>> ds
<xarray.Dataset> Size: 104B
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 16B 0 1
    a        (x) int64 16B 3 4
  * y        (y) int64 24B 0 1 2
Data variables:
    v        (x, y) float64 48B 1.0 1.0 1.0 1.0 1.0 1.0
>>> ds.set_index(x="a")
<xarray.Dataset> Size: 88B
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) int64 16B 3 4
  * y        (y) int64 24B 0 1 2
Data variables:
    v        (x, y) float64 48B 1.0 1.0 1.0 1.0 1.0 1.0
```

<div id="6b3765ad-6ae8-4e98-8d30-09536a2cad5a" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f82fa6b3-6260-4151-bc8a-4bbd51b0e75e','6b3765ad-6ae8-4e98-8d30-09536a2cad5a')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f82fa6b3-6260-4151-bc8a-4bbd51b0e75e','6b3765ad-6ae8-4e98-8d30-09536a2cad5a')">Open In Tab</button></div><div id="50cfb93f-d4f9-47f4-aac7-c347be879beb" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`Dataset.reset_index`](xarray.Dataset.reset_index.html.md#xarray.Dataset.reset_index), [`Dataset.set_xindex`](xarray.Dataset.set_xindex.html.md#xarray.Dataset.set_xindex), [`Dataset.swap_dims`](xarray.Dataset.swap_dims.html.md#xarray.Dataset.swap_dims)
