# coding = utf-8
import numpy as np
def nan_fill(a):
for i in range(a.shape[1]):
temp_col = a[:, i]
count_col_nan = np.count_nonzero(temp_col == temp_col)
if count_col_nan != 0:
not_nan_num = temp_col[temp_col == temp_col]
temp_col[np.isnan(temp_col)] = not_nan_num.mean()
return a

if __name__ == '__main__':
a = np.arange(12).reshape(3, 4).astype(float)
print(a)
a[1, 2:] = np.nan
print(a)
a = nan_fill(a)
print(a)


效果图,依次为原数组,含有nan的数组,替换均值后的数组:

numpy_将nan替换为均值

 

相关文章:

  • 2022-01-01
  • 2021-11-20
  • 2022-12-23
  • 2021-11-29
  • 2021-06-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-01-16
  • 2021-12-17
  • 2022-12-23
  • 2022-02-09
相关资源
相似解决方案