三元运算符 np.where,裁剪clip以及处理缺失值

 np.where(t<5,0,1)的意思就是:把t中小于5的替换成0,其他的替换成1.

 t.clip(5,8)的意思就是:把t中小于5的替换为5,大于8的替换为8。

一个用列平均值来替换nan的小例子:

 1 import numpy as np
 2 def fill_ndarray(t1):
 3     for i in range(t1.shape[1]):   #一列一列的循环
 4         temp_col = t1[:,i]
 5         nan_num = np.count_nonzero(temp_col!=temp_col)
 6         if nan_num !=0:  #不为0 说明当前这一列有nan
 7             tem_not_nan_col = temp_col[temp_col==temp_col] #当前一列不为nan的array
 8             #选中当前nan的位置,替换为当前列不为nan的值的均值
 9             temp_col[np.isnan(temp_col)] = tem_not_nan_col.mean()
10     return t1
11 
12 if __name__ == '__main__':
13     t1 = np.arange(12).reshape(3,4).astype('float')
14     t1[1,2:] = np.nan
15     t2 = fill_ndarray(t1)
16     print(t2)
--------------------成功,肯定是需要一点一滴积累的--------------------
原文地址:https://www.cnblogs.com/GouQ/p/12593450.html