如果你想得到每一行的真/假,你可以使用下面的代码。以下是以下 DataFrame 的结果示例:
df = pd.DataFrame([[None, 3], ["", np.nan]])
df
# 0 1
#0 None 3.0
#1 NaN
如何查看None
可用:.isnull()
>>> df[0].isnull()
0 True
1 False
Name: 0, dtype: bool
可用:.apply == 或 is None
>>> df[0].apply(lambda x: x == None)
0 True
1 False
Name: 0, dtype: bool
>>> df[0].apply(lambda x: x is None)
0 True
1 False
Name: 0, dtype: bool
可用:.values==None
>>> df[0].values == None
array([ True, False])
不可用:is 或 ==
>>> df[0] is None
False
>>> df[0] == None
0 False
1 False
Name: 0, dtype: bool
不可用:.values is None
>>> df[0].values is None
False
如何查看np.nan
可用:.isnull()
>>> df[1].isnull()
0 False
1 True
Name: 1, dtype: bool
可用:np.isnan
>>> np.isnan(df[1])
0 False
1 True
Name: 1, dtype: bool
>>> np.isnan(df[1].values)
array([False, True])
>>> df[1].apply(lambda x: np.isnan(x))
0 False
1 True
Name: 1, dtype: bool
不可用:is 或 == np.nan
>>> df[1] is np.nan
False
>>> df[1] == np.nan
0 False
1 False
Name: 1, dtype: bool
>>> df[1].values is np.nan
False
>>> df[1].values == np.nan
array([False, False])
>>> df[1].apply(lambda x: x is np.nan)
0 False
1 False
Name: 1, dtype: bool
>>> df[1].apply(lambda x: x == np.nan)
0 False
1 False
Name: 1, dtype: bool