如何在 Xarray 中添加另一个坐标标签?

发布于 2025-01-09 20:06:29 字数 295 浏览 2 评论 0原文

我有一个 Xarray,说

da = xr.DataArray([[1,2],[1,2]],coords={'x':['param1','param2'],'y':['idx','idx2']})

这是一个 2D 数据数组。 当我构建代码时,我想向这个 2D 数据集添加另一个“列”或坐标标签。类似于在 Pandas 中我可以

da['param3'] = [1,2]

添加其他列。 Xarray中有等效的方法吗?

I have an Xarray, say

da = xr.DataArray([[1,2],[1,2]],coords={'x':['param1','param2'],'y':['idx','idx2']})

This is a 2D data array. As I build out my code, I would like to add another "column" or coordinate label to this 2D dataset. Similar to how in Pandas I can just do

da['param3'] = [1,2]

To add that other column. Is there an equivalent method in Xarray?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2025-01-16 20:06:29

我首先使用新数据创建第二个 dataArray:

da2 = xr.DataArray([[1,2]],coords={'x':['param3'],'y':['idx','idx2']})

然后将它们组合起来

da3 = xr.combine_by_coords([da, da2])

你会得到

<xarray.DataArray (x: 3, y: 2)>
array([[1, 2],
       [1, 2],
       [1, 2]])
Coordinates:
  * x        (x) <U6 'param1' 'param2' 'param3'
  * y        (y) <U4 'idx' 'idx2'

I would first create a second dataArray with the new data:

da2 = xr.DataArray([[1,2]],coords={'x':['param3'],'y':['idx','idx2']})

then combine them

da3 = xr.combine_by_coords([da, da2])

You get

<xarray.DataArray (x: 3, y: 2)>
array([[1, 2],
       [1, 2],
       [1, 2]])
Coordinates:
  * x        (x) <U6 'param1' 'param2' 'param3'
  * y        (y) <U4 'idx' 'idx2'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文