how to select variables in a dataset using xarray code example

Example: how to select variables in a dataset using xarray

#import xarray
import xarray as xr

#open the dataset
ds = xr.open_dataset(file_name.nc)

#var_name in the following table is to be entered as a string

selection         |    syntax                     |    returns
--------------------------------------------------------------
single variable   | ds[var_name]                  | DataArray
--------------------------------------------------------------
single variable   | ds[[var_name]]                | Dataset
--------------------------------------------------------------
multiple variable | ds[[var_name1, var_name2...]] | Dataset

Tags:

Misc Example