如何从具有浮点值的2D numpy数组中删除NaN,并且结果的类型应为数组?

2023-02-06 13:55:41 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,如果我的2D数组(df_nump)如下所示:

array([[0.78, 0.806, 0.837, 0.831, 0.813, 0.802, 0.792, 0.773, 0.769,
        0.72, 0.676, 0.606, 0.428, nan, nan, nan, nan, nan, nan, nan,
        nan, nan, nan, nan, nan, nan, nan, nan],
       [0.866, 0.911, 0.921, 0.923, 0.906, 0.893, 0.881, 0.86, 0.841,
        0.803, 0.782, 0.702, 0.602, nan, nan, nan, nan, nan, nan, nan,
        nan, nan, nan, nan, nan, nan, nan, nan]])

期望的结果是

array([[0.78, 0.806, 0.837, 0.831, 0.813, 0.802, 0.792, 0.773, 0.769,
            0.72, 0.676, 0.606, 0.428],
           [0.866, 0.911, 0.921, 0.923, 0.906, 0.893, 0.881, 0.86, 0.841,
            0.803, 0.782, 0.702, 0.602,]])

我应该如何转变

我发现使用df_nump=[[I for I in row if~np.isnan(I)]for row in df_nump]但是键入结果是一个列表,我想要数组类型 谢谢


Tags: in类型df列表for键入ifnp数组nanarrayrownumpisnan