Skip to content

Easy way to calculate maximum across multiple variables in a dataset #6321

Answered by dcherian
gewitterblitz asked this question in Q&A
Discussion options

You must be logged in to vote

Dataset.to_array is very useful for this kind of thing. then call idxmax on the DataArray.

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@gewitterblitz
Comment options

Answer selected by gewitterblitz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants