Python pandas:选择列值为空/无/南的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40245507/
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-19 23:20:34  来源:igfitidea点击:

Python pandas: selecting rows whose column value is null / None / nan

pythonpandas

提问by zadrozny

How do I select those rows of a DataFrame whose value in a column is none?

如何选择列中值为 none 的 DataFrame 的那些行?

I've coded these to np.nanand can't match against this type.

我已经将这些编码为np.nan并且无法与这种类型匹配。

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: df = pd.DataFrame([[1, 2, 3], [3, 4, None]])

In [4]: df
Out[4]: 
   0  1    2
0  1  2  3.0
1  3  4  NaN

In [5]: df = df.fillna(np.nan)

In [6]: df
Out[6]: 
   0  1    2
0  1  2  3.0
1  3  4  NaN

In [7]: df.iloc[1][2]
Out[7]: nan

In [8]: df.iloc[1][2] == np.nan
Out[8]: False

In [9]: df[df[2] == None]
Out[9]: 
Empty DataFrame
Columns: [0, 1, 2]
Index: []

回答by MaxU

you can use .isna()method:

您可以使用.isna()方法:

In [48]: df[df[2].isna()]
Out[48]:
   0  1   2
1  3  4 NaN