Last answered:

10 Oct 2022

Posted on:

01 Aug 2022

0

Result not reflecting for filling missing values

Hello I am trying to replace my missing values directly using the np.nan option but my result are not working why?
Below is my code:

DataMissing2[:,0] = np.where(DataMissing2[:,0] == np.nan, ColumnMeans[0],DataMissing2[:,0])
1 answers ( 0 marked as helpful)
Posted on:

10 Oct 2022

0

I think comparing missing values with '==' returns a false boolean
Also, I think there is no function called 'ColumnMeans'

Alternatively, you can make either of the following

1- Pass the value you want to replace in genfromtxt through filling_value parameter, and then use where function to replace your value in specific columns of the dataset

2- Simply use 'np.nan_to_num' function
so the code should be

DataMissing2[:,0] = np.nan_to_num(DataMissing2[:,0], np.nanmean(DataMissing2, axis=0)[0])



3- Instead of using 'DataMissing2[:,0] == np.nan== np.nan', use 'isnan' as follow


DataMissing2[:,0] = np.where(np.isnan(DataMissing2[:,0]), np.nanmean(DataMissing2, axis=0)[0], DataMissing2[:,0])

Submit an answer