0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【xarray】複数の次元を1次元に結合(平坦化)する (stack)

Last updated at Posted at 2023-09-07

この記事では、以下のライブラリを用いる。

import xarray as xr

DataArray.stack()

DataArrayのメソッドstack()を使うと、複数の次元を1次元に結合することができる(平坦化)。

次のDataArrayを例として説明する。

values = [['a','b','c'],['d','e','f']]
dims = ['x','y']
coords = {'x': [0,1], 'y': [0,1,2]}
array = xr.DataArray(values, dims=dims, coords=coords)

array

image.png

使い方①

stack()の引数に、結合後の名前と結合したい次元を指定する。

stacked_array = array.stack(position=['x','y'])
stacked_array

image.png

使い方②

DataArrayのメソッドunstack()を使うと、stack()で結合した次元をもとに戻すことができる。

unstacked_array = stacked_array.unstack('position')
unstacked_array

image.png

公式ドキュメント

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?