# 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#L4842-L5004)

Set Dataset (multi-)indexes using one or more existing coords 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.

#### 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)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('c9b33e31-53ad-4825-b658-464e21b52ad1','e5bff15b-22e4-4e12-9d4a-e4b3aa376049','5ed66cd0-d9c5-497f-b1fc-c9eceaf0fd37','../lite/tree/../notebooks/index.html?path=59d23011_c657_46c0_befd_fb509b5ef302.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="5ed66cd0-d9c5-497f-b1fc-c9eceaf0fd37" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('c9b33e31-53ad-4825-b658-464e21b52ad1','5ed66cd0-d9c5-497f-b1fc-c9eceaf0fd37')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('c9b33e31-53ad-4825-b658-464e21b52ad1','5ed66cd0-d9c5-497f-b1fc-c9eceaf0fd37')">Open In Tab</button></div><div id="e5bff15b-22e4-4e12-9d4a-e4b3aa376049" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
