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

Minahil Noor 2023年1月30日 2021年1月22日
  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