In this post, we are going to learn how to replace nan with zero in NumPy array, replace nan with values,numpy to replace nan with mean,numpy replaces inf with zero by using the built-in function Numpy Library. To run this program make sure NumPy is already installed.
1. Numpy replaces nan with zero in numpy array
The numpy.nan_to_num() function is used whenever it need to replace nan(not a number) values. It replaces nan values with zero and inf with a finite number in an array. We pass numpy array to nan_to_num() function to replace nan values with zero.This is how to replace nan with zero.
- import numpy library by using “import numpy as np”
- Call the numpy library function nan_to_num() to replace nan with zero
import numpy as np
ndarray = np.array([[ 5,np.nan, 15, 45], [ 9, 7, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
print(np.nan_to_num(ndarray))
Output
[[ 5. 0. 15. 45.]
[ 9. 7. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
2. Numpy replace nan with 1
In this python program, we are using nan_to_num() we can pass int, float values to nan parameter of nan_to_num() function to replace nan values with the given value. In this python program example, we will learn how to replace nan with 1 in a numpy array.
import numpy as np
ndarray = np.array([[ 5,np.nan, 15, 45], [ 9, 7, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
print(np.nan_to_num(ndarray, nan=1))
Output
[[ 5. 1. 15. 45.]
[ 9. 7. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 1.]
[20. 7. 21. 1.]]
3. How to replace nan with zero in NumPy array
The isnan() is used to check if the numpy array contains nan values or not. In the case of replacing nan with isnan() return a boolean array that contains True where nan value exists and false for not nan value.
import numpy as np
nparr = np.array([[ 5,np.nan, 15, 45], [ 9, np.nan, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
nparr[np.isnan(nparr)] = 0
print(nparr)
Output
[[ 5. 0. 15. 45.]
[ 9. 0. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
4.np.where() : numpy Replace nan with value
In this example, we are replacing nan with value 9. We can replace nan with any custom value as per need. We will learn how to replace nan with a value. We can replace nan with string value by passing string value instead of value 9.
import numpy as np
nparr = np.array([[ 5,np.nan, 15, 45], [ 9, np.nan, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
resarr = np.where(np.isnan(nparr), 9, nparr)
print(resarr)
Output
[[ 5. 9. 15. 45.]
[ 9. 9. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 9.]
[20. 7. 21. 9.]]
5.np.genfromtxt() : numpy replace nan value with zero
if sometimes we have to read data from CSV files by using np.genfromtxt() function filling_values parameter we can assign value with which we want to replace nan value. In this example, we are replacing nan with zero
import numpy as np
nparr = np.array([[ 5,np.nan, 15, 45], [ 9, np.nan, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
nparr = np.genfromtxt('student.csv', delimiter=',', filling_values=0)
print(nparr)
Output
[[ 3. 9. 8. 0.]
[ 9. 7. 0. 60.]
[16. 0. 19. 70.]
[18. 0. 20. 80.]]
6. Numpy replace nan with mean
In this python script, we will discuss how to replace nan with mean in a numpy array. We have used the NumPy function nanmean() where we have replaced nan value with a mean value of numpy array.
import numpy as np
nparr = np.array([[ 5,np.nan, 15, 45], [ 9, np.nan, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.nan],
[20, 7, 21, np.nan]])
print('mean:',np.nanmean(nparr))
#replace nan with mean
print(np.nan_to_num(nparr, nan=np.nanmean(nparr)))
Output
mean : 23.25
[[ 5. 23.25 15. 45. ]
[ 9. 23.25 11. 60. ]
[16. 10. 19. 70. ]
[18. 26. 20. 23.25]
[20. 7. 21. 23.25]]