首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将xarray数据变量重新赋值到xarray坐标

将xarray数据变量重新赋值到xarray坐标
EN

Stack Overflow用户
提问于 2020-01-10 05:52:17
回答 1查看 529关注 0票数 1

我有一个pandas空间数据帧,我想将其转换为netCDF。我已经找到了使用xarray的方法,并将我的dataframe转换为xarray数据集:

代码语言:javascript
复制
# create xray Dataset from Pandas DataFrame
xr = xarray.Dataset.from_dataframe(df)

现在,我想将lonlat变量设置为我的xarray数据集的坐标。我已经尝试过xarray.Dataset.assign_coords,但似乎不能让它工作?

我的xarray数据集如下所示:

代码语言:javascript
复制
<xarray.Dataset>
Dimensions:  (index: 58705)
Coordinates:
  * index    (index) int64 0 1 2 3 4 5 6 ... 58699 58700 58701 58702 58703 58704
Data variables:
    x_km     (index) float64 5.274e+03 5.273e+03 ... 2.873e+03 2.873e+03
    y_km     (index) float64 0.0 46.02 92.03 138.0 ... -75.23 -50.15 -25.07 -0.0
    z_km     (index) float64 3.575e+03 3.575e+03 ... 1.947e+03 1.947e+03
    dv_v     (index) float64 0.2407 0.1774 0.1786 ... -0.2163 -0.2035 -0.3197
    rxy      (index) float64 5.274e+03 5.273e+03 ... 2.873e+03 2.873e+03
    lon      (index) float64 0.0 0.5 1.0 1.5 2.0 ... -2.0 -1.5 -1.0 -0.5 -0.0
    lat      (index) float64 34.13 34.13 34.13 34.13 ... 34.11 34.12 34.12 34.13
    rxyz     (index) float64 6.371e+03 6.371e+03 ... 3.471e+03 3.471e+03
    depth    (index) float64 0.04665 0.04747 0.04766 ... 2.9e+03 2.9e+03 2.9e+03
Attributes:
    Conventions:  CF-1.6
    title:        Data
    summary:      Data generated

感谢任何帮助:D

EN

回答 1

Stack Overflow用户

发布于 2020-01-10 10:54:08

从一个名为dsDataset开始,如下所示:

代码语言:javascript
复制
Dimensions:  (index: 10)
Coordinates:
  * index    (index) int64 0 1 2 3 4 5 6 7 8 9
Data variables:
    dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
    rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
    lon      (index) int64 15 7 9 17 18 1 12 2 6 8
    lat      (index) int64 6 8 5 17 15 16 9 19 11 14
    rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
    depth    (index) int64 11 18 5 19 3 14 7 17 0 4

可以使用ds.set_coords(("lat", "lon"))latlon转换为坐标。结果如下所示:

代码语言:javascript
复制
Dimensions:  (index: 10)
Coordinates:
  * index    (index) int64 0 1 2 3 4 5 6 7 8 9
    lon      (index) int64 15 7 9 17 18 1 12 2 6 8
    lat      (index) int64 6 8 5 17 15 16 9 19 11 14
Data variables:
    dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
    rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
    rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
    depth    (index) int64 11 18 5 19 3 14 7 17 0 4

另一个类似的(但不等价的)替代方法是使用ds.set_index(index=("lat", "lon")),它将index修改为具有索引latlon的多级索引。输出将如下所示:

代码语言:javascript
复制
Dimensions:  (index: 10)
Coordinates:
  * index    (index) MultiIndex
  - lat      (index) int64 6 8 5 17 15 16 9 19 11 14
  - lon      (index) int64 15 7 9 17 18 1 12 2 6 8
Data variables:
    dv_v     (index) int64 5 14 6 1 19 12 16 10 0 11
    rxy      (index) int64 15 8 6 2 0 1 4 16 7 19
    rxyz     (index) int64 15 17 18 5 14 13 16 2 10 9
    depth    (index) int64 11 18 5 19 3 14 7 17 0 4
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59672658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档