Issue
I am building a simple null identifier function for identifying nan in Numpy arrays (not Pandas DataFrames or Series), but my code does not seem to work, even though after going over it multiple times I still see no problems with the syntax. What you guys think? Thank you The array:
X = np.array([[1,2,4,np.nan],[3,np.nan,4,2],[5,1,6,7],[np.nan,1,2,9]])
The iteration:
for i in range(X.shape[0]):
for j in range(X.shape[1]):
if X[i,j] == np.nan:
print("This entry is null")
else:
print("This one is not")
It returns: "This one is not" sixteen times (the size of the array). In other words, no missing values apparently. What's wrong?
Solution
It is noted in numpy documentation cannot use equality to test NaNs by such modules. You must use np.isnan
as follows:
for i in range(len(X)):
for j in range(X.shape[1]):
if np.isnan(X[i, j]):
print("This entry is null")
else:
print("This one is not")
Answered By - Ali_Sh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.