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

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

问题描述

我有这个 DataFrame 并且只想要 EPS 列不是 NaN 的记录:

<预><代码>>>>dfSTK_ID EPS 现金STK_ID RPT_Date601166 20111231 601166 南南600036 20111231 600036 NaN 12600016 20111231 600016 4.3 NaN601009 20111231 601009 南南601939 20111231 601939 2.5 NaN000001 20111231 000001 NaN NaN

...即类似于 df.drop(....) 来获取这个结果数据帧:

 STK_ID EPS 现金STK_ID RPT_Date600016 20111231 600016 4.3 NaN601939 20111231 601939 2.5 NaN

我该怎么做?

解决方案

不要丢,只取 EPS 不是 NA 的行:

df = df[df['EPS'].notna()]

I have this DataFrame and want only the records whose EPS column is not NaN:

>>> 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

...i.e. something like df.drop(....) to get this resulting dataframe:

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

How do I do that?

解决方案

Don't drop, just take the rows where EPS is not NA:

df = df[df['EPS'].notna()]

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

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