Check if numpy array contains any NaN value
In this article, we will see how to check whether the numpy array contains NaN value or not.
Introduction
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
NaN is not a number. We can create NaN by using numpy.nan.
Method 1 : Using isnan() from numpy module
We can check whether the numpy array contains NaN values or not by using any() operator through isnan() operator(available in numpy module).
We have to check this by using an condition.any() will check if there are any NaN values
Syntax:
if(numpy.isnan(array_input).any()):
print("Has NaN values")
else:
print("No NaN values")
Example:
In this example, we will create an numpy array with 10 elements (3 NaN among 10 elements) and check the NaN values.
#import numpy module
import numpy
#create an numpy array with 10 elements that includes three NaN values
array_input=numpy.array([12,34,56,78,numpy.nan,numpy.nan,10,10,numpy.nan,123])
#display array
print(array_input)
#check the array has NaN values or not.
if(numpy.isnan(array_input).any()):
print("Has NaN values")
else:
print("No NaN values")
Output:
It contains three NaN values, so it printed NaN values are present(if block is executed).
[ 12. 34. 56. 78. nan nan 10. 10. nan 123.]
Has NaN values
Method 2 : Using isna() from pandas module
We can check whether the numpy array contains NaN values or not by using any() operator through isna() operator(available in pandas module).
We have to check this by using an condition.any() will check if there are any NaN values
Syntax:
if(pandas.isna(array_input).any()):
print("Has NaN values")
else:
print("No NaN values")
Example:
In this example, we will create an numpy array with 10 elements (3 NaN among 10 elements) and check the NaN values.
#import numpy module
import numpy
#import pandas module
import pandas
#create an numpy array with 10 elements that includes three NaN values
array_input=numpy.array([12,34,56,78,numpy.nan,numpy.nan,10,10,numpy.nan,123])
#display array
print(array_input)
#check the array has NaN values or not.
if(pandas.isna(array_input).any()):
print("Has NaN values")
else:
print("No NaN values")
Output:
It contains three NaN values, so it printed NaN values are present(if block is executed).
[ 12. 34. 56. 78. nan nan 10. 10. nan 123.]
Has NaN values
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 12,2023