Python pandas: how to remove nan and -inf values

前端 未结 6 544
自闭症患者
自闭症患者 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:03

    You can replace inf and -inf with NaN, and then select non-null rows.

    df[df.replace([np.inf, -np.inf], np.nan).notnull().all(axis=1)]  # .astype(np.float64) ?
    

    or

    df.replace([np.inf, -np.inf], np.nan).dropna(axis=1)
    

    Check the type of your columns returns to make sure they are all as expected (e.g. np.float32/64) via df.info().

提交回复
热议问题