Python pandas: how to remove nan and -inf values

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

    Instead of dropping rows which contain any nulls and infinite numbers, it is more succinct to the reverse the logic of that and instead return the rows where all cells are finite numbers. The numpy isfinite function does this and the '.all(1)' will only return a TRUE if all cells in row are finite.

    df = df[np.isfinite(df).all(1)]
    

提交回复
热议问题