網站首頁 編程語言 正文
數據準備
import pandas as pd
df = pd.DataFrame([['ABC','Good',1],
['FJZ',None,2],
['FOC','Good',None]
],columns=['Site','Remark','Quantity'])
df
注意:上述Remark字段中的數據類型為字符串str類型,空值取值為'None',Quantity字段中的數據類型為數值型,空值取值為nan?
1.篩選指定單列中有空值的數據行
# 語法
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
提示
篩選指定單列中沒有空值的數據行
# 語法
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.篩選指定多列中/全部列中滿足所有列有空值的數據行?
# 語法
df[df[[cols]].isnull().all(axis=1)]
df[pd.isnull(df[[cols]]).all(axis=1)]
在df基礎上增加一行生成df1
df1 = pd.DataFrame([['ABC','Good',1],
['FJZ',None,2],
['FOC','Good',None],
[None,None,None]
],columns=['Site','Remark','Quantity'])
# 獲取df1所有列有空值的數據行
all_df_isnull = df1[df1[['Site','Remark','Quantity']].isnull().all(axis=1)]
all_df_isnull
提示
篩選指定多列中/全部列中滿足所有列沒有空值的數據行?
# 語法
df[df[[cols]].notnull().all(axis=1)]
df[pd.notnull(df[[cols]]).all(axis=1)]
# 獲取df1所有列沒有空值的數據行
all_df_notnull = df1[df1[['Site','Remark','Quantity']].notnull().all(axis=1)]
all_df_notnull
3.篩選指定多列中/全部列中滿足任意一列有空值的數據行?
# 語法
df[df[[cols]].isnull().any(axis=1)]
df[pd.isnull(df[[cols]]).any(axis=1)]
df1(數據源)
# 獲取df1所有列中滿足任意一列有空值的數據行
any_df_isnull = df1[df1[['Site','Remark','Quantity']].isnull().any(axis=1)]
any_df_isnull
提示
篩選指定多列中/全部列中滿足任意一列沒有空值的數據行
# 語法
df[df[[cols]].notnull().any(axis=1)]
df[pd.notnull(df[[cols]]).any(axis=1)]
# 獲取df1所有列中滿足任意一列沒有空值的數據行
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']
新增數據列后的df?
res1 = df[df['site2'].isnull()]
res2 = df[df['site2'].isna()]
res3 = df[df['site2']=='']
res1
res2
res3
注意:res1和res2的結果相同,說明.isna()和.isnull()的作用等效
原文鏈接:https://blog.csdn.net/Hudas/article/details/125351275
相關推薦
- 2022-10-23 React路由中的redux和redux知識點拓展_React
- 2023-03-17 Go語言依賴管理三要素示例解析_Golang
- 2022-12-08 Apache?Cordova?Android原理應用實例詳解_Android
- 2022-07-12 k8s conntrack 表項超時導致tcp長連接中斷
- 2022-05-12 Python繪制計算機CPU占有率變化的折線圖_python
- 2022-12-31 Python中CSV文件的讀寫庫操作方法_python
- 2023-01-03 一文帶你掌握Go語言中文件的寫入操作_Golang
- 2022-10-25 python繪圖之坐標軸的超詳細講解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支