1 Problem
I am practicing 100 Numpy exercises. Question 60 asks for how to tell if a given 2D array has null columns?
I am wondering whether it asks for checking a column fulled with 0 or fulled with nan?
2 Solutions I found
If null columns stands for column with its value all equal to 0
, this answer satisfies.
# Author: Warren Weckesser
Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis=0)).any())
It uses a trick of any()
. Noticing that if one of values in an array is not equal to 0, np.array().any()
will return True. For instance:
np.array([0,-1,0]).any()
[Out]: True
However, if null columns stands for column with its value all equal to nan
, then my answer satisfies.
Z = np.random.randint(0,3,(3,10)).astype(float)
Z[:,0] = np.nan
G = np.isnan(Z)
print((G.all(axis=0)).any())
3 Summary
What does null
means in python? I think is means nan
, but it conflicts with Author(Warren Weckesser)'s answer.