获取具有NaN值的所有行

MJP*_*MJP 11 python numpy pandas

我有一个表格,其中包含一些NaN值:

A   B   C   D
2   3   2   Nan
3   4   5   5
2   3   1   Nan
Run Code Online (Sandbox Code Playgroud)

我想获得D = NaN的所有行.我怎样才能做到这一点?

Nip*_*tra 24

创建一个df用于说明(包含Nan)

In [86]: df =pd.DataFrame({'a':[1,2,3],'b':[3,4,5],'c':[np.nan, 4,5]})

In [87]: df
Out[87]: 
   a  b   c
0  1  3 NaN
1  2  4   4
2  3  5   5
Run Code Online (Sandbox Code Playgroud)

检查列c的哪些索引为空

In [88]: pd.isnull(df['c'])
Out[88]: 
0     True
1    False
2    False
Name: c, dtype: bool
Run Code Online (Sandbox Code Playgroud)

检查列c的哪些索引不为null

In [90]: pd.notnull(df['c'])
Out[90]: 
0    False
1     True
2     True
Name: c, dtype: bool
Run Code Online (Sandbox Code Playgroud)

选择df的行,其中c不为null

In [91]: df[pd.notnull(df['c'])]
Out[91]: 
   a  b  c
1  2  4  4
2  3  5  5
Run Code Online (Sandbox Code Playgroud)

选择df的行,其中c为null

In [93]: df[pd.isnull(df['c'])]
Out[93]: 
   a  b   c
0  1  3 NaN
Run Code Online (Sandbox Code Playgroud)

选择df列c的行,其中c不为null

In [94]: df['c'][pd.notnull(df['c'])]
Out[94]: 
1    4
2    5
Name: c, dtype: float64
Run Code Online (Sandbox Code Playgroud)