迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

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

作者:迹忆客 最近更新:2024/04/21 浏览次数:

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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便