Pandas篩選DataFrame含有空值的數(shù)據(jù)行的實現(xiàn)
數(shù)據(jù)準(zhǔn)備
import pandas as pd
df = pd.DataFrame([['ABC','Good',1],
['FJZ',None,2],
['FOC','Good',None]
],columns=['Site','Remark','Quantity'])df

注意:上述Remark字段中的數(shù)據(jù)類型為字符串str類型,空值取值為'None',Quantity字段中的數(shù)據(jù)類型為數(shù)值型,空值取值為nan
1.篩選指定單列中有空值的數(shù)據(jù)行
# 語法 df[pd.isnull(df[col])] df[df[col].isnull()]
# 獲取Remark字段為None的行 df_isnull_remark = df[df['Remark'].isnull()] # 獲取Quantity字段為None的行 df_isnull_quantity = df[df['Quantity'].isnull()]
df_isnull_remark

df_isnull_quantity

提示
篩選指定單列中沒有空值的數(shù)據(jù)行
# 語法 df[pd.notnull(df[col])] df[df[col].notnull()]
# 獲取Remark字段為非None的行 df_notnull_remark = df[df['Remark'].notnull()] # 獲取Quantity字段為非None的行 df_notnull_quantity = df[df['Quantity'].notnull()]
df_notnull_remark

df_notnull_quantity

2.篩選指定多列中/全部列中滿足所有列有空值的數(shù)據(jù)行
# 語法 df[df[[cols]].isnull().all(axis=1)] df[pd.isnull(df[[cols]]).all(axis=1)]
在df基礎(chǔ)上增加一行生成df1
df1 = pd.DataFrame([['ABC','Good',1],
['FJZ',None,2],
['FOC','Good',None],
[None,None,None]
],columns=['Site','Remark','Quantity'])
# 獲取df1所有列有空值的數(shù)據(jù)行 all_df_isnull = df1[df1[['Site','Remark','Quantity']].isnull().all(axis=1)]
all_df_isnull

提示
篩選指定多列中/全部列中滿足所有列沒有空值的數(shù)據(jù)行
# 語法 df[df[[cols]].notnull().all(axis=1)] df[pd.notnull(df[[cols]]).all(axis=1)]
# 獲取df1所有列沒有空值的數(shù)據(jù)行 all_df_notnull = df1[df1[['Site','Remark','Quantity']].notnull().all(axis=1)]
all_df_notnull

3.篩選指定多列中/全部列中滿足任意一列有空值的數(shù)據(jù)行
# 語法 df[df[[cols]].isnull().any(axis=1)] df[pd.isnull(df[[cols]]).any(axis=1)]
df1(數(shù)據(jù)源)

# 獲取df1所有列中滿足任意一列有空值的數(shù)據(jù)行 any_df_isnull = df1[df1[['Site','Remark','Quantity']].isnull().any(axis=1)]
any_df_isnull

提示
篩選指定多列中/全部列中滿足任意一列沒有空值的數(shù)據(jù)行
# 語法 df[df[[cols]].notnull().any(axis=1)] df[pd.notnull(df[[cols]]).any(axis=1)]
# 獲取df1所有列中滿足任意一列沒有空值的數(shù)據(jù)行 any_df_notnull = df1[df1[['Site','Remark','Quantity']].notnull().any(axis=1)]
any_df_notnull

Numpy里邊查找NaN值的話,使用np.isnan()
Pabdas里邊查找NaN值的話,使用.isna()或.isnull()
import pandas as pd
import numpy as np
df = pd.DataFrame({'site1': ['a', 'b', 'c', ''],
'site2': ['a', np.nan, '', 'd'],
'site3': ['a', 'b', 'c', 'd']})df

df['contact_site'] = df['site1'] + df['site2'] + df['site3']
新增數(shù)據(jù)列后的df

res1 = df[df['site2'].isnull()] res2 = df[df['site2'].isna()] res3 = df[df['site2']=='']
res1

res2

res3

注意:res1和res2的結(jié)果相同,說明.isna()和.isnull()的作用等效
到此這篇關(guān)于Pandas篩選DataFrame含有空值的數(shù)據(jù)行的實現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas篩選DataFrame空值行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django初步使用Celery處理耗時任務(wù)和定時任務(wù)問題
這篇文章主要介紹了Django初步使用Celery處理耗時任務(wù)和定時任務(wù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
pymongo如何通過oplog獲取數(shù)據(jù)(mongodb)
使用MongoDB的oplog(操作日志)進行數(shù)據(jù)同步是高級的用法,主要用于復(fù)制和故障恢復(fù),這篇文章主要介紹了pymongo通過oplog獲取數(shù)據(jù)(mongodb),需要的朋友可以參考下2023-09-09
使用Pytorch+PyG實現(xiàn)MLP的詳細(xì)過程
圖神經(jīng)網(wǎng)絡(luò)是最近 AI 領(lǐng)域最熱門的方向之一,下面這篇文章主要給大家介紹了關(guān)于使用Pytorch+PyG實現(xiàn)MLP的詳細(xì)過程,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
pandas中DataFrame字典互轉(zhuǎn)的實現(xiàn)
在數(shù)據(jù)處理和分析中,Pandas是一個非常強大的Python庫,本文主要介紹了pandas中DataFrame字典互轉(zhuǎn)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04

