Remove all rows that contain non-numeric values from an NumPy array

In this python numpy program, we will remove all rows that contain non-numeric values from a NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Remove all rows that contain non-numeric values from the array using array[~np.isnan(array).any(axis=1)].
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8, np.nan],[2,4,6]])
print("Old: \n",x)

print("New: \n",x[~np.isnan(x).any(axis=1)])
				
			

Output :

				
					Old: 
 [[ 4.  8. nan]
 [ 2.  4.  6.]]
New: 
 [[2. 4. 6.]]
				
			

replace the negative values in an array with 1.

get the magnitude of a vector.

Leave a Comment