How can I check for NaN values?

后端 未结 17 2111
盖世英雄少女心
盖世英雄少女心 2020-11-22 05:02

float(\'nan\') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it.

17条回答
  •  一整个雨季
    2020-11-22 05:56

    Here are three ways where you can test a variable is "NaN" or not.

    import pandas as pd
    import numpy as np
    import math
    
    #For single variable all three libraries return single boolean
    x1 = float("nan")
    
    print(f"It's pd.isna  : {pd.isna(x1)}")
    print(f"It's np.isnan  : {np.isnan(x1)}")
    print(f"It's math.isnan : {math.isnan(x1)}")
    

    Output

    It's pd.isna  : True
    It's np.isnan  : True
    It's math.isnan  : True
    

提交回复
热议问题