DataFrame如何找出有空值的行
更新時間:2024年02月02日 10:31:09 作者:大地之燈
這篇文章主要介紹了DataFrame如何找出有空值的行問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
DataFrame找出有空值的行
import pandas as pdimport numpy as np
n = np.arange(20, dtype=float).reshape(5,4) n[2,3] = np.nan index = ['index1', 'index2', 'index3', 'index4', 'index5'] columns = ['column1', 'column2', 'column3', 'column4'] frame3 = pd.DataFrame(data=n, index=index, columns=columns)
frame3
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | 0.0 | 1.0 | 2.0 | 3.0 |
index2 | 4.0 | 5.0 | 6.0 | 7.0 |
index3 | 8.0 | 9.0 | 10.0 | NaN |
index4 | 12.0 | 13.0 | 14.0 | 15.0 |
index5 | 16.0 | 17.0 | 18.0 | 19.0 |
frame3.isnull()
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | False | False | False | False |
index2 | False | False | False | False |
index3 | False | False | False | True |
index4 | False | False | False | False |
index5 | False | False | False | False |
# any() 作用:返回是否至少一個元素為真 # 直接求any(),得到的每一列求any()計算的結果 frame3.isnull().any()
column1 False column2 False column3 False column4 True dtype: bool
判斷有空值的行
# 方法一:設置any的axis參數(shù) frame3.isnull().any(axis = 1)
index1 False index2 False index3 True index4 False index5 False dtype: bool
# 方法二:先轉置再any frame3.isnull().T.any()
index1 False index2 False index3 True index4 False index5 False dtype: bool
應用:取非空值的行
frame3[frame3.isnull().any(axis = 1)==False]
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index1 | 0.0 | 1.0 | 2.0 | 3.0 |
index2 | 4.0 | 5.0 | 6.0 | 7.0 |
index4 | 12.0 | 13.0 | 14.0 | 15.0 |
index5 | 16.0 | 17.0 | 18.0 | 19.0 |
應用:取有空值的行
frame3[frame3.isnull().any(axis = 1)==True]
column1 | column2 | column3 | column4 | |
---|---|---|---|---|
index3 | 8.0 | 9.0 | 10.0 | NaN |
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python gevent協(xié)程切換實現(xiàn)詳解
這篇文章主要介紹了Python gevent協(xié)程切換實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09對Python中DataFrame選擇某列值為XX的行實例詳解
今天小編就為大家分享一篇對Python中DataFrame選擇某列值為XX的行實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python實現(xiàn)圖像檢索的三種(直方圖/OpenCV/哈希法)
這篇文章主要介紹了python實現(xiàn)圖像檢索的三種(直方圖/OpenCV/哈希法),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08使用python-pptx創(chuàng)建PPT演示文檔功能實踐
這篇文章主要介紹了使用python-pptx創(chuàng)建PPT演示文檔功能實踐,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06舉例講解Linux系統(tǒng)下Python調用系統(tǒng)Shell的方法
這篇文章主要介紹了舉例講解Linux系統(tǒng)下Python調用系統(tǒng)Shell的方法,包括用Python和shell讀取文件某一行的實例,需要的朋友可以參考下2015-11-11Python報錯:OSError:?[Errno?22]?Invalid?argument解決方案及應用實例
最近跑別人的項目遇到一個這樣的問題一開始以為是沒有用管理員的權限運行,導致創(chuàng)建不了日志文件后來發(fā)現(xiàn)是和windows的命名規(guī)則沖突了,這篇文章主要給大家介紹了關于Python報錯:OSError:?[Errno?22]?Invalid?argument的解決方案及應用實例,需要的朋友可以參考下2024-07-07