# xarray.Dataset.assign

#### Dataset.assign(variables=None, \*\*variables_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L7135-L7244)

Assign new data variables to a Dataset, returning a new object
with all the original variables in addition to the new ones.

* **Parameters:**
  * **variables** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of [hashable](../user-guide/terminology.html.md#term-name) to `Any`) – Mapping from variables names to the new values. If the new values
    are callable, they are computed on the Dataset and assigned to new
    data variables. If the values are not callable, (e.g. a DataArray,
    scalar, or array), they are simply assigned.
  * **\*\*variables_kwargs** – The keyword arguments form of `variables`.
    One of variables or variables_kwargs must be provided.
* **Returns:**
  **ds** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – A new Dataset with the new variables in addition to all the
  existing variables.

### Notes

Since `kwargs` is a dictionary, the order of your arguments may not
be preserved, and so the order of the new variables is not well
defined. Assigning multiple variables within the same `assign` is
possible, but you cannot reference other variables created within the
same `assign` call.

The new assigned variables that replace existing coordinates in the
original dataset are still listed as coordinates in the returned
Dataset.

#### SEE ALSO
[`pandas.DataFrame.assign`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html#pandas.DataFrame.assign)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('3ed61186-110a-44fe-950c-3e465ae4dc7a','7f080d94-614a-4f7c-9f05-8544575a5ac7','fab38bf4-3491-4237-bdfe-f431446e1aed','../lite/tree/../notebooks/index.html?path=aea82c36_161e_4e3d_82d1_5a93603b19ec.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> x = xr.Dataset(
...     {
...         "temperature_c": (
...             ("lat", "lon"),
...             20 * np.random.rand(4).reshape(2, 2),
...         ),
...         "precipitation": (("lat", "lon"), np.random.rand(4).reshape(2, 2)),
...     },
...     coords={"lat": [10, 20], "lon": [150, 160]},
... )
>>> x
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
```

Where the value is a callable, evaluated on dataset:

```pycon
>>> x.assign(temperature_f=lambda x: x.temperature_c * 9 / 5 + 32)
<xarray.Dataset> Size: 128B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 32B 51.76 57.75 53.7 51.62
```

Alternatively, the same behavior can be achieved by directly referencing an existing dataarray:

```pycon
>>> x.assign(temperature_f=x["temperature_c"] * 9 / 5 + 32)
<xarray.Dataset> Size: 128B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 32B 51.76 57.75 53.7 51.62
```

<div id="fab38bf4-3491-4237-bdfe-f431446e1aed" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('3ed61186-110a-44fe-950c-3e465ae4dc7a','fab38bf4-3491-4237-bdfe-f431446e1aed')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('3ed61186-110a-44fe-950c-3e465ae4dc7a','fab38bf4-3491-4237-bdfe-f431446e1aed')">Open In Tab</button></div><div id="7f080d94-614a-4f7c-9f05-8544575a5ac7" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
