find max value of a list with numpy nan [duplicate]

那年仲夏 提交于 2019-12-23 07:10:24

问题


import numpy as np

print max([np.nan, 1, 2, 3, 4])
print max([1, 2, 3, 4, np.nan])
print max([1, 2, 3, np.nan, 4])

the first will print nan as list's max value

the second will print 4 as list's max value

the third will print 4 as list's max value

Is there a solution for this problem? Make all math function just ignore nan?


回答1:


Use np.nanmax() to ignore any NaNs:

In [57]: np.nanmax([np.nan, 1, 2, 3, 4])
Out[57]: 4.0


来源:https://stackoverflow.com/questions/43042424/find-max-value-of-a-list-with-numpy-nan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!