# xarray.Dataset.reduce

#### Dataset.reduce(func, dim=None, , keep_attrs=None, keepdims=False, numeric_only=False, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L6904-L7026)

Reduce this dataset by applying func along some dimension(s).

* **Parameters:**
  * **func** ([`callable()`](https://docs.python.org/3/builtins/functions.html#callable)) – Function which can be called in the form
    f(x, axis=axis, \*\*kwargs) to return the result of reducing an
    np.ndarray over an integer valued axis.
  * **dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), `Iterable` of `Hashable` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dimension(s) over which to apply func. By default func is
    applied over all dimensions.
  * **keep_attrs** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – If True (default), the dataset’s attributes (attrs) will be copied from
    the original object to the new one.  If False, the new
    object will be returned without attributes.
  * **keepdims** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, the dimensions which are reduced are left in the result
    as dimensions of size one. Coordinates that use these dimensions
    are removed.
  * **numeric_only** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, only apply `func` to variables with a numeric dtype.
  * **\*\*kwargs** (`Any`) – Additional keyword arguments passed on to `func`.
* **Returns:**
  **reduced** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – Dataset with this object’s DataArrays replaced with new DataArrays
  of summarized data and the indicated dimension(s) removed.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('59b5f013-8e37-49dd-a325-be4200b13027','ba991a48-39b8-43ac-bdc5-30a150c068f9','fc73b86a-2a04-4d1a-bf01-777d239c3222','../lite/tree/../notebooks/index.html?path=f8470fb3_0c86_460c_8fee_6f43c9149a80.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> dataset = xr.Dataset(
...     {
...         "math_scores": (
...             ["student", "test"],
...             [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
...         ),
...         "english_scores": (
...             ["student", "test"],
...             [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
...         ),
...     },
...     coords={
...         "student": ["Alice", "Bob", "Charlie"],
...         "test": ["Test 1", "Test 2", "Test 3"],
...     },
... )
```

# Calculate the 75th percentile of math scores for each student using np.percentile

```pycon
>>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test")
>>> percentile_scores
<xarray.Dataset> Size: 132B
Dimensions:         (student: 3)
Coordinates:
  * student         (student) <U7 84B 'Alice' 'Bob' 'Charlie'
Data variables:
    math_scores     (student) float64 24B 91.0 82.5 96.5
    english_scores  (student) float64 24B 91.0 80.5 94.5
```

<div id="fc73b86a-2a04-4d1a-bf01-777d239c3222" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('59b5f013-8e37-49dd-a325-be4200b13027','fc73b86a-2a04-4d1a-bf01-777d239c3222')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('59b5f013-8e37-49dd-a325-be4200b13027','fc73b86a-2a04-4d1a-bf01-777d239c3222')">Open In Tab</button></div><div id="ba991a48-39b8-43ac-bdc5-30a150c068f9" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
