Python 如何过滤 NaN (pandas)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25050141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:39:20  来源:igfitidea点击:

How to filter in NaN (pandas)?

pythonpandasnan

提问by Gerhard

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

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

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

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

我试过用np.NaN,'NaN''nan'等替换 NaN ,但没有任何评估为 True 。没有pd.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.

我可以df.fillna(np.nan)在评估上述表达式之前使用,但这感觉很hackish,我想知道它是否会干扰其他依赖于以后能够识别pandas格式NaN的pandas操作。

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.

我觉得这个问题应该有一个简单的答案,但不知何故,它让我望而却步。任何建议表示赞赏。谢谢你。

采纳答案by Mark Whitfield

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

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

回答by NicholasM

Pandas uses numpy's NaN value. Use numpy.isnanto obtain a Boolean vector from a pandas series.

Pandas 使用numpy的 NaN 值。用于numpy.isnan从熊猫系列中获取布尔向量。

回答by Gil Baggio

Simplest of all solutions:

最简单的解决方案:

filtered_df = df[df['var2'].isnull()]

This filters and gives you rows which has only NaN values in 'var2' column.

这会过滤并为您提供在“var2”列中只有 NaN 值的行。