Easy way to calculate maximum across multiple variables in a dataset #6321
Answered
by
dcherian
gewitterblitz
asked this question in
Q&A
-
Hi, I have a spatiotemporal dataset (3D space and 1D time) with multiple variables (same dimensions and units) and I would like to calculate which variable has the highest value in each grid cell at each time step. Here's an example: What's the best way to go about doing such an operation? |
Beta Was this translation helpful? Give feedback.
Answered by
dcherian
Mar 2, 2022
Replies: 1 comment 1 reply
-
In [2]: ds = xr.Dataset(
...: {"a": ("x", [0, 1, 2, 3]), "b": ("x", [-1, 2, 0, 0]), "c": ("x", [0, 1, 4, 5])}
...: )
In [3]: ds.to_array("variable").idxmax("variable")
Out[3]:
<xarray.DataArray 'variable' (x: 4)>
array(['a', 'b', 'c', 'c'], dtype='<U1')
Dimensions without coordinates: x |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
gewitterblitz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dataset.to_array
is very useful for this kind of thing. then callidxmax
on the DataArray.