Fix Python – convert nan value to zero

I have a 2D numpy array. Some of the values in this array are NaN. I want to perform certain operations using this array. For example consider the array:
[[ 0. 43. 67. 0. 38.]
[ 100. 86. 96. 100. 94.]
[ 76. 79. 83. 89. 56.]
[ 88. NaN 67. 89. 81.]
[ 94. 79. 67. 89. 69.]
[ 88. 79. 58. 72. 63….

Fix Python – How to filter in NaN (pandas)?

I have a pandas dataframe (df), and I want to do something like:
newdf = df[(df.var1 == ‘a’) & (df.var2 == NaN)]

I’ve tried replacing NaN with np.NaN, or ‘NaN’ or ‘nan’ etc, but nothing evaluates to True. There’s no pd.NaN.
I can use df.fillna(np.nan) before evaluating the above expression but that feels hackish and I wonder if it will interfere ….

Fix Python – Fast check for NaN in NumPy

I’m looking for the fastest way to check for the occurrence of NaN (np.nan) in a NumPy array X. np.isnan(X) is out of the question, since it builds a boolean array of shape X.shape, which is potentially gigantic.
I tried np.nan in X, but that seems not to work because np.nan != np.nan. Is there a fast and memory-efficient way to do this at all?
(T….

Fix Python – How to set a cell to NaN in a pandas dataframe

I’d like to replace bad values in a column of a dataframe by NaN’s.
mydata = {‘x’ : [10, 50, 18, 32, 47, 20], ‘y’ : [’12’, ’11’, ‘N/A’, ’13’, ’15’, ‘N/A’]}
df = pd.DataFrame(mydata)

df[df.y == ‘N/A’][‘y’] = np.nan

Though, the last line fails and throws a warning because it’s working on a copy of df. So, what’s the correct way to handle this? I’v….