Search code examples
pythondatasetpython-xarraydimensions

How to transform a set of xarray dataset variables into a single variable with additional dimension


I have the below xarray Dataset. Now, I'd like to combine Fender_1, Fender_2, and Fender_3 variables into a single variable called "data" with the dimension "fender_name". Such that I can simply call the variable "data" and select which fender I want using data.sel(fender_name = ...)

How can I achieve this?

xarray dataset


Solution

  • One way to do this would be to use Dataset.to_array:

    data = ds[["Fender_1", "Fender_2", "Fender_3"]].to_array(dim="fender_name")