Dataset: how to select a few Data variables and drop the rest

3,890 views
Skip to first unread message

Ryan Yan

unread,
Jul 12, 2017, 11:16:57 AM7/12/17
to xarray
Hello everyone,
I am wondering if anyone happens to know to select a few Data variables and drop the rest (most of the all).
For instance, in the following example at http://xarray.pydata.org/en/stable/examples/quick-overview.html

In [38]:
ds.to_netcdf('example.nc') In [39]: xr.open_dataset('example.nc') Out[39]: <xarray.Dataset> Dimensions: (x: 2, y: 3) Coordinates: * x (x) object 'a' 'b' Dimensions without coordinates: y Data variables: baz float64 3.142 foo (x, y) float64 -1.295 0.4137 0.2767 -0.472 -0.01396 -0.3625 bar (x) int64 1 2


In [40]: ds2 = xr.open_dataset('example.nc')

, how can we just select the data variables 'foo' and 'bar' for the dataset ds2. Thank you very much for your helps.

Best regards,
Yan

Stephan Hoyer

unread,
Jul 12, 2017, 11:25:14 AM7/12/17
to xarray
You can do this by indexing with a list of desired variables:

ds2 = ds[['foo', 'bar']]

--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/6a5a089d-1950-4383-a366-1eb15c1aa91f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Yan

unread,
Jul 12, 2017, 1:09:39 PM7/12/17
to xarray
Hi Stephan,
Thank you very much for your help.
The approach you suggested works very well. Many thanks!
Best regards,
Yan
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages