【问题标题】:How to replace all values of an xarray with a constant (say 5) in Python?如何在 Python 中用常量(比如 5)替换 xarray 的所有值?
【发布时间】:2019-10-31 20:45:00
【问题描述】:

我知道如何使用.where 根据here 之类的条件替换值,但我想知道如何用数字替换xarray 中的所有值。

提前致谢

【问题讨论】:

    标签: python python-xarray


    【解决方案1】:

    根据文档here,您所要做的就是:

    a = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])
    
    b = a.where(a > 5, other=5)
    b = b.where(b < 5, other=5)
    

    正如 r-beer 所建议的那样,一种直接的方法是:

    a = a.where(a == 5, other=5)
    

    【讨论】:

      【解决方案2】:

      使用以下代码可能更直接:

      a = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])
      
      c = a.where(a == 5, other=5)
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-26
        • 2014-12-17
        • 2016-03-15
        • 1970-01-01
        • 2019-07-30
        • 1970-01-01
        相关资源
        最近更新 更多