Reputation: 3563
How do you select a column from an xarray.DataArray
please? This is how I am doing the xarray
import xarray as xr
import numpy as np
import pandas as pd
my_data = np.random.rand(5,2)
da = xr.DataArray(my_data,
coords={'my_id':np.arange(my_data.shape[0]),
'columns': ['x', 'y']},
dims=['my_id', 'columns'])
and I want to select column x
. If that was a dataframe
df = pd.DataFrame(my_data, columns=['x','y'])
i am looking for the equivalent of df['x']
Am I defining the DataArray
the wrong way?
Upvotes: 2
Views: 2314
Reputation: 27869
You can use pandas-like .loc
as suggested in docs:
da.loc[:, ['x']]
<xarray.DataArray (my_id: 5, columns: 1)>
array([[ 0.534358],
[ 0.113875],
[ 0.905085],
[ 0.96994 ],
[ 0.548338]])
Coordinates:
* my_id (my_id) int32 0 1 2 3 4
* columns (columns) <U1 'x'
Upvotes: 1
Reputation: 1052
Straight from the docs: http://xarray.pydata.org/en/stable/generated/xarray.DataArray.to_pandas.html
import xarray as xr
import numpy as np
import pandas as pd
my_data = np.random.rand(5,2)
da = xr.DataArray(my_data,
coords={'my_id':np.arange(my_data.shape[0]),
'columns': ['x', 'y']},
dims=['my_id', 'columns'])
df = da.to_pandas()
df['x']
Result
df['x']
Out[27]:
my_id
0 0.752056
1 0.048029
2 0.145835
3 0.083147
4 0.618351
Name: x, dtype: float64
Upvotes: 0