Replacing or Removing Missing Values in NumPy

NumPy provides methods to handle missing values (np.nan) by replacing or removing them.

1. Importing NumPy

Before handling missing values, import NumPy.

            import numpy as np
        

2. Replacing Missing Values

Use np.nan_to_num() or indexing to replace np.nan values.

Using np.nan_to_num()

            arr = np.array([1, 2, np.nan, 4, np.nan, 6])
            
            replaced_arr = np.nan_to_num(arr, nan=0)
            print(replaced_arr)
            # Output: [1. 2. 0. 4. 0. 6.]
        

Replacing NaN Values with Mean

            arr = np.array([1, 2, np.nan, 4, np.nan, 6])
            
            mean_value = np.nanmean(arr)
            arr[np.isnan(arr)] = mean_value
            print(arr)
            # Example Output: [1. 2. 3.25 4. 3.25 6.]
        

3. Removing Missing Values

Use boolean indexing to filter out np.nan values.

            arr = np.array([1, 2, np.nan, 4, np.nan, 6])
            
            cleaned_arr = arr[~np.isnan(arr)]
            print(cleaned_arr)
            # Output: [1. 2. 4. 6.]
        

4. Conclusion

NumPy provides efficient ways to replace or remove missing values using np.nan_to_num() and filtering techniques.





Advertisement