Python pandas: how to remove nan and -inf values

前端 未结 6 553
自闭症患者
自闭症患者 2020-12-02 12:10

I have the following dataframe

           time       X    Y  X_t0     X_tp0  X_t1     X_tp1  X_t2     X_tp2
0         0.002876    0   10     0       NaN   Na         


        
6条回答
  •  鱼传尺愫
    2020-12-02 13:02

    I prefer to set the options so that inf values are calculated to nan;

    s1 = pd.Series([0, 1, 2])
    s2 = pd.Series([2, 1, 0])
    s1/s2
    # Outputs:
    # 0.0
    # 1.0
    # inf
    # dtype: float64
    
    pd.set_option('mode.use_inf_as_na', True)
    s1/s2
    # Outputs:
    # 0.0
    # 1.0
    # NaN
    # dtype: float64
    

    Note you can also use context;

    with pd.option_context('mode.use_inf_as_na', True):
        print(s1/s2)
    # Outputs:
    # 0.0
    # 1.0
    # NaN
    # dtype: float64
    

提交回复
热议问题