[英]xarray.dataset: re-shuffle variables (not coordinates) so that they appear in certain order with print or ncdump
I was wondering if there exists the possibility of re-shuffling variables in an xarray.dataset...我想知道是否存在重新洗牌 xarray.dataset 中的变量的可能性......
I have a dataset where I've been appending variables following a certain order given by a certain process... Then, when I print the dataset in a Python console (or when executing ncdump -h output.nc
I get that same order, but I wish to get variables in another order.我有一个数据集,我一直在按照某个进程给出的某个顺序附加变量...然后,当我在 Python 控制台中打印数据集时(或执行
ncdump -h output.nc
时,我得到了相同的顺序,但我希望以另一种顺序获取变量。
What I get:我得到什么:
<xarray.Dataset>
Dimensions: (stations: 6,
Rrs_bands: 110,
Rrs_S3A_OLCI_bands: 21)
Coordinates:
* stations (stations) <U16 ...
* Rrs_bands (Rrs_bands) float64 ...
* Rrs_S3A_OLCI_bands (Rrs_S3A_OLCI_bands) float64 ...
Data variables: (12/69)
Rrs (stations, Rrs_bands) float64 ...
acquisition_time (stations) object ...
lon (stations) float64 ...
lat (stations) float64 ...
depth (stations) float64 ...
Rrs_qa (stations) float64 ...
... ...
total_column_formaldehyde (stations) float64 ...
total_column_hydrogen_peroxide (stations) float64 ...
total_column_hydroxyl_radical (stations) float64 ...
total_column_nitric_acid (stations) float64 ...
total_column_peroxyacetyl_nitrate (stations) float64 ...
Rrs_S3A_OLCI (stations, Rrs_S3A_OLCI_bands) float64 ...
Attributes:
description: In situ database file (IDB) for insitu set Geneva (in sit...
What I want:我想要的是:
<xarray.Dataset>
Dimensions: (stations: 6,
Rrs_bands: 110,
Rrs_S3A_OLCI_bands: 21)
Coordinates:
* stations (stations) <U16 ...
* Rrs_bands (Rrs_bands) float64 ...
* Rrs_S3A_OLCI_bands (Rrs_S3A_OLCI_bands) float64 ...
Data variables: (12/69)
acquisition_time (stations) object ...
lon (stations) float64 ...
lat (stations) float64 ...
depth (stations) float64 ...
... ...
total_column_formaldehyde (stations) float64 ...
total_column_hydrogen_peroxide (stations) float64 ...
total_column_hydroxyl_radical (stations) float64 ...
total_column_nitric_acid (stations) float64 ...
total_column_peroxyacetyl_nitrate (stations) float64 ...
Rrs (stations, Rrs_bands) float64 ...
Rrs_qa (stations) float64 ...
Rrs_S3A_OLCI (stations, Rrs_S3A_OLCI_bands) float64 ...
Attributes:
description: In situ database file (IDB) for insitu set Geneva (in sit...
Is there an easy way of re-organizing this listing when printing or doing ncdump?在打印或执行 ncdump 时,是否有一种简单的方法可以重新组织此列表?
Thanks:)谢谢:)
yep!是的!
ds = ds[list_of_variables_in_desired_order]
Datasets are built on (ordered) dictionaries of data variables.数据集建立在数据变量的(有序)字典之上。 If you create a new dataset object with keys in the desired order, the result will be a copy which preserves the order in which you pass the variables.
如果您创建一个新的数据集 object 并使用所需顺序的键,结果将是一个副本,其中保留了您传递变量的顺序。
To specify the order of coordinates and variables at the same time, pass the coordinates as well:要同时指定坐标和变量的顺序,还要传递坐标:
ds = ds[
list_of_coordinates_in_desired_order
+ list_of_variables_in_desired_order
]
So in your case:所以在你的情况下:
ds = ds[[
"stations",
"Rrs_bands",
"Rrs_S3A_OLCI_bands",
"acquisition_time",
"lon",
"lat",
"depth",
..., # complete the list of other variables
"total_column_formaldehyde",
"total_column_hydrogen_peroxide",
"total_column_hydroxyl_radical",
"total_column_nitric_acid",
"total_column_peroxyacetyl_nitrate",
"Rrs",
"Rrs_qa",
"Rrs_S3A_OLCI",
]]
See the API docs for xr.Dataset.__getitem__
:有关
xr.Dataset.__getitem__
的信息,请参阅 API 文档:
Dataset.__getitem__(key)
Access variables or coordinates of this dataset as aDataArray
or a subset of variables or a indexed dataset.Dataset.__getitem__(key)
以DataArray
或变量子集或索引数据集的形式访问此数据集的变量或坐标。Indexing with a list of names will return a new
Dataset
object.使用名称列表进行索引将返回一个新的
Dataset
object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.