如何过滤NaN( pandas )? [英] How to filter in NaN (pandas)?

查看:65
本文介绍了如何过滤NaN( pandas )?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框(df),我想做类似的事情:

I have a pandas dataframe (df), and I want to do something like:

newdf = df[(df.var1 == 'a') & (df.var2 == NaN)]

我尝试用np.NaN'NaN''nan'等替换NaN,但没有结果为True.没有pd.NaN.

I've tried replacing NaN with np.NaN, or 'NaN' or 'nan' etc, but nothing evaluates to True. There's no pd.NaN.

在评估上述表达式之前,我可以使用df.fillna(np.nan),但这感觉有些骇人听闻,我想知道它是否会干扰依赖于以后识别熊猫格式NaN的其他熊猫操作.

I can use df.fillna(np.nan) before evaluating the above expression but that feels hackish and I wonder if it will interfere with other pandas operations that rely on being able to identify pandas-format NaN's later.

我觉得对这个问题应该有一个简单的答案,但是以某种方式它使我难以理解.任何建议表示赞赏.谢谢.

I get the feeling there should be an easy answer to this question, but somehow it has eluded me. Any advice is appreciated. Thank you.

推荐答案

这不起作用,因为NaN不等于任何东西,包括NaN.请使用pd.isnull(df.var2).

This doesn't work because NaN isn't equal to anything, including NaN. Use pd.isnull(df.var2) instead.

这篇关于如何过滤NaN( pandas )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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