# xarray.Dataset.filter_by_attrs

#### Dataset.filter_by_attrs(\*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L8899-L8992)

Returns a `Dataset` with variables that match specific conditions.

Can pass in `key=value` or `key=callable`.  A Dataset is returned
containing only the variables for which all the filter tests pass.
These tests are either `key=value` for which the attribute `key`
has the exact value `value` or the callable passed into
`key=callable` returns True. The callable will be passed a single
value, either the value of the attribute `key` or `None` if the
DataArray does not have an attribute with the name `key`.

* **Parameters:**
  **\*\*kwargs** –

  key
  : Attribute name.

  value
  : If value is a callable, it should return a boolean in the form
    of bool = func(attr) where attr is da.attrs[key].
    Otherwise, value will be compared to the each
    DataArray’s attrs[key].
* **Returns:**
  **new** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – New dataset with variables filtered by attribute.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('b8721358-d499-4e51-9700-ff1dc22e644f','90c7367b-5157-4076-a69d-ce06a9bffc9b','5c5910ae-ffc9-4b65-8c65-369f54057141','../lite/tree/../notebooks/index.html?path=7eb9825f_8df8_466b_b4f2_dd07563e6a98.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> temp = 15 + 8 * np.random.randn(2, 2, 3)
>>> precip = 10 * np.random.rand(2, 2, 3)
>>> lon = [[-99.83, -99.32], [-99.79, -99.23]]
>>> lat = [[42.25, 42.21], [42.63, 42.59]]
>>> dims = ["x", "y", "time"]
>>> temp_attr = dict(standard_name="air_potential_temperature")
>>> precip_attr = dict(standard_name="convective_precipitation_flux")
```

```pycon
>>> ds = xr.Dataset(
...     dict(
...         temperature=(dims, temp, temp_attr),
...         precipitation=(dims, precip, precip_attr),
...     ),
...     coords=dict(
...         lon=(["x", "y"], lon),
...         lat=(["x", "y"], lat),
...         time=pd.date_range("2014-09-06", periods=3),
...         reference_time=pd.Timestamp("2014-09-05"),
...     ),
... )
```

Get variables matching a specific standard_name:

```pycon
>>> ds.filter_by_attrs(standard_name="convective_precipitation_flux")
<xarray.Dataset> Size: 192B
Dimensions:         (x: 2, y: 2, time: 3)
Coordinates:
    lon             (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 32B 42.25 42.21 42.63 42.59
  * time            (time) datetime64[us] 24B 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[us] 8B 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    precipitation   (x, y, time) float64 96B 5.68 9.256 0.7104 ... 4.615 7.805
```

Get all variables that have a standard_name attribute:

```pycon
>>> standard_name = lambda v: v is not None
>>> ds.filter_by_attrs(standard_name=standard_name)
<xarray.Dataset> Size: 288B
Dimensions:         (x: 2, y: 2, time: 3)
Coordinates:
    lon             (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 32B 42.25 42.21 42.63 42.59
  * time            (time) datetime64[us] 24B 2014-09-06 2014-09-07 2014-09-08
    reference_time  datetime64[us] 8B 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 96B 29.11 18.2 22.83 ... 16.15 26.63
    precipitation   (x, y, time) float64 96B 5.68 9.256 0.7104 ... 4.615 7.805
```

<div id="5c5910ae-ffc9-4b65-8c65-369f54057141" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('b8721358-d499-4e51-9700-ff1dc22e644f','5c5910ae-ffc9-4b65-8c65-369f54057141')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('b8721358-d499-4e51-9700-ff1dc22e644f','5c5910ae-ffc9-4b65-8c65-369f54057141')">Open In Tab</button></div><div id="90c7367b-5157-4076-a69d-ce06a9bffc9b" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
