numpy 从xarray DataArray删除零

sh7euo9m  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(189)

我有一个有三个坐标的DataArray,我想从其中一个维度删除包含所有零的数组,但我似乎无法正确处理。数据数组(old_array)如下所示,正如你所看到的,有相当多的数组填充了零。

<xarray.DataArray (dim1: 100, dim2: 100, dim3: 100)>
array([[[81.20, 13.20, 12.24, ..., 81.24,
         14.61, 85.58],
        [31.34, 83.96, 02.97, ..., 46.88,
         01.81, 48.24],
        [43.54, 12.50, 35.33, ..., 39.23,
         91.93, 01.21],
        [37.95, 75.59, 71.00, ..., 15.14,
         68.76, 44.56],
        [54.31, 48.99, 85.59, ..., 15.58,
         86.48, 81.19]],

       [[ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
...                                
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ]],           
                                   
       [[ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ],            
        [ 0.  ,  0.  ,  0.  , ...,  0.  ,
          0.  ,  0.  ]]])
Coordinates:
  * dim1  (dim1) <U7 'x0 y0 z0' 'x1 y0 z0' 'x2 y0 z0' ... 'x10 y10 z8' 'x10 y10 z9' 'x10 y10 z10'
  * dim2  (dim2) float64 0.0 0.1 0.2 ... 9.8 9.9 10.0
  * dim3  (dim3) float64 0.5 1.0 1.5 ... 49.0 49.5 50.0

我试过了

new_array = old_array.drop_sel(dim1 = np.zeros(100))

但它给了我KeyError:“在索引'dim1'中未找到所有值”
我也试过

new_array = old_array.drop(np.zeros(100), dim1)

但是我有一个ValueError:尺寸'x1y5 z3'没有坐标标签。
我想告诉xarray“如果'x2 y3 z7'是一个全零的数组,那么删除它”,但我不知道怎么做。

wvt8vs2t

wvt8vs2t1#

这仍然有点难以判断你想做什么,但从我可以告诉,你想放弃数组中等于零的部分。为此,你应该使用DataArray.wheredrop=True

old_array.where(old_array > 0, drop=True)

drop_sel用于根据其值删除索引标签。

相关问题