Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing all elements except NaN in Python

I want to replace all elements in array X except nan with 10.0. Is there a one-step way to do it? I present the expected output.

import numpy as np
from  numpy import nan

X = np.array([[3.25774286e+02, 3.22008654e+02, nan, 1.85356823e+02,
        1.85356823e+02, 3.22008654e+02, nan, 3.22008654e+02]])

The expected output is

X = array([[10.0, 10.0, nan, 10.0,
        10.0, 10.0, nan, 10.0]])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a combination of numpy.isnan and numpy.where.

>>> np.where(np.isnan(X), X, 10)
array([[10., 10., nan, 10., 10., 10., nan, 10.]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading