如何删除某些列中的值为NaN的Pandas DataFrame行 [英] How to drop rows of Pandas DataFrame whose value in certain columns is NaN

查看:1035
本文介绍了如何删除某些列中的值为NaN的Pandas DataFrame行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataFrame

>>> df
                 STK_ID  EPS  cash
STK_ID RPT_Date                   
601166 20111231  601166  NaN   NaN
600036 20111231  600036  NaN    12
600016 20111231  600016  4.3   NaN
601009 20111231  601009  NaN   NaN
601939 20111231  601939  2.5   NaN
000001 20111231  000001  NaN   NaN

然后我只是希望 EPS 不是 NaN 的记录,即 df.drop(.. ..)将返回数据框如下:

Then I just want the records whose EPS is not NaN, that is, df.drop(....) will return the dataframe as below:

                  STK_ID  EPS  cash
STK_ID RPT_Date                   
600016 20111231  600016  4.3   NaN
601939 20111231  601939  2.5   NaN

这样做?

推荐答案

不要 drop 。只需在 EPS 之间

df = df[np.isfinite(df['EPS'])]

这篇关于如何删除某些列中的值为NaN的Pandas DataFrame行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆