Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all values of an xarray with a constant (say 5) in Python?

I know how to replace values based on a condition like here using .where but I would like to know how to replace all values in an xarray with a number.

Thanks in advance

like image 562
Shruthi Patil Avatar asked Oct 26 '25 07:10

Shruthi Patil


1 Answers

It might be more straight forward to use the following code:

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

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

like image 65
r-beer Avatar answered Oct 28 '25 20:10

r-beer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!