# coding=utf-8 import numpy as np ''' 填充nan的数据,为该列的平均值 ''' def fill_ndarray(t1): for i in range(t1.shape[1]): temp_col = t1[:,i] #取每一列 print(temp_col) nan_num =np.count_nonzero(temp_col!=temp_col) #判断该列存在不为0的数个数 if( nan_num != 0 ): temp_not_nan_col = temp_col[temp_col==temp_col] temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean() return t1 if __name__ == '__main__': t1 = np.random.randint(10, 18, (3, 4)).astype(float) print(t1) t1[1, 2:] = np.nan print(t1) t2 = fill_ndarray(t1) print(t2)