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

A   B   C   D
2   3   2   Nan
3   4   5   5
2   3   1   Nan

我想把D=nan的所有行都找出来。我该怎么做?

最佳答案

创建用于说明的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

检查C列的哪些索引为空
In [88]: pd.isnull(df['c'])
Out[88]:
0     True
1    False
2    False
Name: c, dtype: bool

检查C列的哪些索引没有空值
In [90]: pd.notnull(df['c'])
Out[90]:
0    False
1     True
2     True
Name: c, dtype: bool

选择c不为空的df行
In [91]: df[pd.notnull(df['c'])]
Out[91]:
   a  b  c
1  2  4  4
2  3  5  5

选择c为空的df行
In [93]: df[pd.isnull(df['c'])]
Out[93]:
   a  b   c
0  1  3 NaN

选择df列c的行,其中c不为空
In [94]: df['c'][pd.notnull(df['c'])]
Out[94]:
1    4
2    5
Name: c, dtype: float64

10-08 08:09
查看更多