# xarray.Dataset.assign

`Dataset.``assign`(variables: Mapping[Hashable, Any] = None, **variables_kwargs) → xarray.core.dataset.Dataset

  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, value pairs) – 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 – A new Dataset with the new variables in addition to all the existing variables.
  Return type: | Dataset

  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.

  See also

  `pandas.DataFrame.assign()`

  Examples

      >>> import numpy as np
      >>> import xarray as xr
      >>> 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>
      Dimensions:        (lat: 2, lon: 2)
      Coordinates:
      * lat            (lat) int64 10 20
      * lon            (lon) int64 150 160
      Data variables:
          temperature_c  (lat, lon) float64 18.04 12.51 17.64 9.313
          precipitation  (lat, lon) float64 0.4751 0.6827 0.3697 0.03524

  Where the value is a callable, evaluated on dataset:

      >>> x.assign(temperature_f = lambda x: x.temperature_c * 9 / 5 + 32)
      <xarray.Dataset>
      Dimensions:        (lat: 2, lon: 2)
      Coordinates:
      * lat            (lat) int64 10 20
      * lon            (lon) int64 150 160
      Data variables:
          temperature_c  (lat, lon) float64 18.04 12.51 17.64 9.313
          precipitation  (lat, lon) float64 0.4751 0.6827 0.3697 0.03524
          temperature_f  (lat, lon) float64 64.47 54.51 63.75 48.76

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

      >>> x.assign(temperature_f=x["temperature_c"] * 9 / 5 + 32)
      <xarray.Dataset>
      Dimensions:        (lat: 2, lon: 2)
      Coordinates:
      * lat            (lat) int64 10 20
      * lon            (lon) int64 150 160
      Data variables:
          temperature_c  (lat, lon) float64 18.04 12.51 17.64 9.313
          precipitation  (lat, lon) float64 0.4751 0.6827 0.3697 0.03524
          temperature_f  (lat, lon) float64 64.47 54.51 63.75 48.76