我有一个pandas空间数据帧,我想将其转换为netCDF。我已经找到了使用xarray的方法,并将我的dataframe转换为xarray数据集:
# create xray Dataset from Pandas DataFrame
xr = xarray.Dataset.from_dataframe(df)现在,我想将lon和lat变量设置为我的xarray数据集的坐标。我已经尝试过xarray.Dataset.assign_coords,但似乎不能让它工作?
我的xarray数据集如下所示:
<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
发布于 2020-01-10 10:54:08
从一个名为ds的Dataset开始,如下所示:
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"))将lat和lon转换为坐标。结果如下所示:
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修改为具有索引lat和lon的多级索引。输出将如下所示:
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 4https://stackoverflow.com/questions/59672658
复制相似问题