Q:

Write a NumPy program to remove nan values from a given array

0

Write a NumPy program to remove nan values from a given array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.array([200, 300, np.nan, np.nan, np.nan ,700])
y = np.array([[1, 2, 3], [np.nan, 0, np.nan] ,[6,7,np.nan]] )
print("Original array:")
print(x)
print("After removing nan values:")
result = x[np.logical_not(np.isnan(x))]
print(result)
print("\nOriginal array:")
print(y)
print("After removing nan values:")
result = y[np.logical_not(np.isnan(y))]
print(result)

Sample Output:

Original array:
[200. 300.  nan  nan  nan 700.]
After removing nan values:
[200. 300. 700.]

Original array:
[[ 1.  2.  3.]
 [nan  0. nan]
 [ 6.  7. nan]]
After removing nan values:
[1. 2. 3. 0. 6. 7.]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now