Pandas DataFrame.isnull()和 notnull()函数

Minahil Noor 2023年1月30日
  1. pandas.DataFrame.isnull()pandas.DataFrame.notnull() 语法
  2. 示例代码:DataFrame.isnull() 方法检查空值
  3. 示例代码:DataFrame.notnull() 方法检查非空值
Pandas DataFrame.isnull()和 notnull()函数

Python Pandas DataFrame.annull() 函数检测对象的缺失值,DataFrame.notnull() 函数检测对象的非缺失值。

pandas.DataFrame.isnull()pandas.DataFrame.notnull() 语法

DataFrame.isnull()
DataFrame.notnull()

返回

对于标量输入,两个函数都返回标量布尔值。对于数组输入,两个函数都返回一个布尔数组,表示每个对应的元素是否有效。

示例代码:DataFrame.isnull() 方法检查空值

import pandas as pd
import numpy as np

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: np.nan, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: np.nan, 1: 75, 2: 82, 3: np.nan, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.isnull()
print("The output is: \n")
print(dataframe1)

输出:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0        60.0  Olivia             NaN
1         NaN    John            75.0
2        80.0   Laura            82.0
3        78.0     Ben             NaN
4        95.0   Kevin            45.0
The output is: 

   Attendance   Name  Obtained Marks
0       False  False            True
1        True  False           False
2       False  False           False
3       False  False            True
4       False  False           False

对于空值,该函数返回 True

示例代码:DataFrame.notnull() 方法检查非空值

import pandas as pd
import numpy as np

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: np.nan, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: np.nan, 1: 75, 2: 82, 3: np.nan, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe1 = dataframe.notnull()
print("The output is: \n")
print(dataframe1)

输出:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0        60.0  Olivia             NaN
1         NaN    John            75.0
2        80.0   Laura            82.0
3        78.0     Ben             NaN
4        95.0   Kevin            45.0
The output is: 

   Attendance  Name  Obtained Marks
0        True  True           False
1       False  True            True
2        True  True            True
3        True  True           False
4        True  True            True

该函数对非空值返回 True

相关文章 - Pandas DataFrame