In this python numpy program, we will remove all rows that contain non-numeric values from a NumPy array.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Remove all rows that contain non-numeric values from the array using array[~np.isnan(array).any(axis=1)].
- 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.]]