英文:
How to use DataArray values as list indexes?
问题
首先,对于标题不太明确,我感到抱歉,我只是找不到更好的标题。我有一个愚蠢的问题,但我已经困在这个问题上好几个小时了。
基本上,我有一个名为data_index
的xarray
DataSet
,其中包含从0到3的整数。这些整数对应于列表的索引。我想在我的DataSet
中添加一个数据变量,该变量与由data_index
变量给定的索引处的列表值相匹配。
以下是我所拥有的内容示例:
import xarray as xr
ds = xr.Dataset()
ds["data_index"] = (("x", "y"), [[0, 1], [2, 3]] )
my_list = ["a", "b", "c", "d"]
我想通过选择与data_index
给定的索引对应的列表值,向数据集ds
中添加一个名为data
的数据变量。类似于:
ds["data"] = my_list[ds["data_index"]]
我的DataSet
比这个大得多。实际的维度是(x: 30001, y: 20001)
。但是变量data_index
仅包含从0到3的整数,而列表也有4个元素。
我相信有一种简单的方法可以做到这一点,但我就是找不到它。你有什么建议吗?
英文:
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?
答案1
得分: 2
要获得结果,您可以使用xarray
提供的isel
方法。它允许您使用基于int
的索引沿特定维度索引数据集。
编辑:
你是对的。我实际上添加了一个名为numpy
的附加库,以帮助重新塑造结果数据数组。
import xarray as xr
import numpy as np
ds = xr.Dataset()
ds["data_index"] = (("x", "y"), [[0, 1], [2, 3]])
my_list = ["a", "b", "c", "d"]
data_values = np.array([my_list[idx] for idx in ds["data_index"].values.flatten()])
data_reshaped = data_values.reshape(ds["data_index"].shape)
ds["data"] = xr.DataArray(data_reshaped, dims=("x", "y"))
print(ds)
英文:
To get the result, you can use the isel
method provided by xarray
. Allowing you to index a dataset along with a particular dimension using int
based indexing.
Edit:
Your right. I've actually added an additional library called numpy
to help with reshaping the resulting data array.
import xarray as xr
import numpy as np
ds = xr.Dataset()
ds["data_index"] = (("x", "y"), [[0, 1], [2, 3]])
my_list = ["a", "b", "c", "d"]
data_values = np.array([my_list[idx] for idx in ds["data_index"].values.flatten()])
data_reshaped = data_values.reshape(ds["data_index"].shape)
ds["data"] = xr.DataArray(data_reshaped, dims=("x", "y"))
print(ds)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论