First of all, I'm sorry for the not very explicit title, I just couldn't find anything better. I have a silly question, but I've been stuck on it for hours.
Basically I have an xarray
DataSet
in which is a data variable called data_index
with integers from 0 to 3. These integers correspond to the indexes of a list. I would like to add a data variable to my DataSet
that matches the list value for the index given by the data_index
variable.
Here is an exemple of what I have :
import xarray as xr
ds = xr.Dataset()
ds["data_index"] = (("x", "y"), [[0, 1], [2, 3]] )
list = ["a", "b" , "c", "d"]
I'd like to add a data variable called data
to the dataset ds by picking the value of the list that correspond to the index data_index. Would be something like :
ds["data"] = list[ds["data_index"]]
My DataSet
is much bigger than that. The real dimensions are (x: 30001, y: 20001)
. But the variable data_index
contains only integers from 0 to 3 and the list is also 4-element long.
I'm sure there is an easy way to do it but I just can't find it. Do you have any leads?