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 DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串