Pandas篩選DataFrame含有空值的數(shù)據(jù)行的實(shí)現(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ù)行的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas篩選DataFrame空值行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+selenium+PhantomJS抓取網(wǎng)頁動(dòng)態(tài)加載內(nèi)容
一般我們使用python的第三方庫(kù)requests及框架scrapy來爬取網(wǎng)上的資源,但是設(shè)計(jì)javascript渲染的頁面卻不能抓取,此 時(shí),我們使用web自動(dòng)化測(cè)試化工具Selenium+無界面瀏覽器PhantomJS來抓取javascript渲染的頁面,下面實(shí)現(xiàn)一個(gè)簡(jiǎn)單的爬取2020-02-02解決Tkinter中button按鈕未按卻主動(dòng)執(zhí)行command函數(shù)的問題
這篇文章主要介紹了解決Tkinter中button按鈕未按卻主動(dòng)執(zhí)行command函數(shù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python中threading.Timer()定時(shí)器實(shí)現(xiàn)定時(shí)任務(wù)
本文主要介紹了Python中threading.Timer()定時(shí)器實(shí)現(xiàn)定時(shí)任務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01django xadmin 管理器常用顯示設(shè)置方式
這篇文章主要介紹了django xadmin 管理器常用顯示設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03卡爾曼濾波數(shù)據(jù)處理技巧通俗理解及python實(shí)現(xiàn)
這篇文章主要為大家介紹了卡爾曼濾波數(shù)據(jù)處理技巧的通俗理解及python實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法
今天小編就為大家分享一篇python 求某條線上特定x值或y值的點(diǎn)坐標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07基于Python實(shí)現(xiàn)在控制臺(tái)查看excel的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)在控制臺(tái)查看excel的內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12