isnull() 函數(shù)用于檢查數(shù)據(jù)框或序列中的缺失值(nan),返回布爾值掩碼,true 為缺失值,false 為有效值。用法包括:檢查數(shù)據(jù)框中的缺失值、檢查序列中的缺失值、使用 isnull() 進(jìn)行條件過(guò)濾。它能幫助識(shí)別數(shù)據(jù)中的缺失值,以便進(jìn)一步處理或分析。
isnull() 函數(shù):Python 中用來(lái)檢查缺失值的函數(shù)
引言
isnull() 函數(shù)是 Python 中 pandas 庫(kù)中用于檢查數(shù)據(jù)框或序列中缺失值(NaN)的函數(shù)。它返回一個(gè)布爾值掩碼,其中為 True 的值表示相應(yīng)位置存在缺失值,為 False 的值表示該位置有有效值。
函數(shù)語(yǔ)法
pandas.isnull(data)
其中:
立即學(xué)習(xí)“Python免費(fèi)學(xué)習(xí)筆記(深入)”;
- data:要檢查缺失值的數(shù)據(jù)框或序列。
用法
使用 isnull() 函數(shù)檢查是否缺失值非常簡(jiǎn)單,只需調(diào)用函數(shù)并傳入要檢查的數(shù)據(jù)即可。以下是 isnull() 函數(shù)的一些常見(jiàn)用法示例:
- 檢查數(shù)據(jù)框中的缺失值:
import pandas as pd data = pd.DataFrame({'name': ['Alice', 'Bob', None], 'age': [20, 30, None]}) print(data.isnull())
輸出:
name age 0 False False 1 False False 2 True True
- 檢查序列中的缺失值:
series = pd.Series([1, 2, None, 4, 5]) print(pd.isnull(series))
輸出:
0 False 1 False 2 True 3 False 4 False dtype: bool
- 使用 isnull() 進(jìn)行條件過(guò)濾:
可以使用 isnull() 函數(shù)對(duì)數(shù)據(jù)框或序列進(jìn)行條件過(guò)濾,以選擇或丟棄包含或不包含缺失值的行或元素。例如:
filtered_data = data[data['age'].isnull()] print(filtered_data)
輸出:
name age 2 None None
總結(jié)
isnull() 函數(shù)是 Python 中檢查數(shù)據(jù)框或序列中缺失值時(shí)非常有用的工具。它允許用戶輕松識(shí)別數(shù)據(jù)中的缺失值,并對(duì)其進(jìn)行進(jìn)一步處理或分析。