How to Remove rows in Numpy array that contains non-numeric values? Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Many times NumPy arrays may contain NaN values that need to be removed to ensure the array is free from unnecessary or invalid data. This can be achieved using the np.isnan() function along with the Bitwise NOT operator. Note that this approach specifically targets NaN values and may not handle other non-numeric characters like strings.Example 1: Python # Importing Numpy module import numpy as np # Creating a 2x3 2-D NumPy array n_arr = np.array([[10.5, 22.5, 3.8], [41, np.nan, np.nan]]) print("Given array:") print(n_arr) print("\nRemove all rows containing NaN values:") cleaned_arr = n_arr[~np.isnan(n_arr).any(axis=1)] print(cleaned_arr) OutputGiven array: [[10.5 22.5 3.8] [41. nan nan]] Remove all rows containing NaN values: [[10.5 22.5 3.8]] In this example, the row containing NaN values is removed from the 2x3 NumPy array.Example 2: Python # Importing Numpy module import numpy as np # Creating a 3x3 2-D NumPy array n_arr = np.array([[10.5, 22.5, 3.8], [23.45, 50, 78.7], [41, np.nan, np.nan]]) print("Given array:") print(n_arr) print("\nRemove all rows containing NaN values:") cleaned_arr = n_arr[~np.isnan(n_arr).any(axis=1)] print(cleaned_arr) OutputGiven array: [[10.5 22.5 3.8 ] [23.45 50. 78.7 ] [41. nan nan]] Remove all rows containing NaN values: [[10.5 22.5 3.8 ] [23.45 50. 78.7 ]] Here, the row with NaN values is removed from the 3x3 NumPy array.Example 3: Python # Importing Numpy module import numpy as np # Creating a 5x4 2-D NumPy array n_arr = np.array([[10.5, 22.5, 3.8, 5], [23.45, 50, 78.7, 3.5], [41, np.nan, np.nan, 0], [20, 50.20, np.nan, 2.5], [18.8, 50.60, 8.8, 58.6]]) print("Given array:") print(n_arr) print("\nRemove all rows containing NaN values:") cleaned_arr = n_arr[~np.isnan(n_arr).any(axis=1)] print(cleaned_arr) OutputGiven array: [[10.5 22.5 3.8 5. ] [23.45 50. 78.7 3.5 ] [41. nan nan 0. ] [20. 50.2 nan 2.5 ] [18.8 50.6 8.8 58.6 ]] Remove all rows containing NaN values: [[10.5 22....In this example, rows with NaN values are removed from the 5x4 NumPy array.Note: This approach is effective for removing rows with NaN values but will not work for other non-numeric characters such as strings. To handle other types of non-numeric data, additional preprocessing might be required. Comment More infoAdvertise with us Next Article How to remove NaN values from a given NumPy array? V vanshgaur14866 Follow Improve Article Tags : Python Python-numpy Python numpy-Logic Functions Practice Tags : python Similar Reads How to Remove columns in Numpy array that contains non-numeric values? Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np.isnan() function. Example 1: Pyth 2 min read How to remove array rows that contain only 0 using NumPy? Numpy library provides a function called numpy.all() that returns True when all elements of n-d array passed to the first parameter are True else it returns False. Thus, to determine the entire row containing 0's can be removed by specifying axis=1. It will traverse each row and will check for the c 2 min read How to remove NaN values from a given NumPy array? In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array. There are basically three approaches with sl 3 min read How to remove rows from a Numpy array based on multiple conditions ? In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on g 3 min read How to read a numerical data or file in Python with numpy? Prerequisites: Numpy NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. This article depicts how numeric data can be read from a file using Numpy. Numerical data can be present in different forma 4 min read Replace NumPy array elements that doesn't satisfy the given condition Sometimes in Numpy array, we want to apply certain conditions to filter out some values and then either replace or remove them. The conditions can be like if certain values are greater than or less than a particular constant, then replace all those values by some other number. For this, we can use R 3 min read Like