網站首頁 編程語言 正文
NaN代表Not A Number,是表示數據中缺失值的常用方法之一。它是一種特殊的浮點值,不能轉換為浮點數以外的任何其他類型。
NaN值是數據分析中的主要問題之一,為了得到理想的結果,對NaN進行處理是非常必要的。
檢查Pandas DataFrame中的NaN值
在Pandas DataFrame中檢查NaN的方法如下:
- 使用isnull().values.any()方法檢查NaN
- 使用isnull().sum()方法統計NaN
- 使用isnull().sum().any()方法檢查NaN
- 使用isnull().sum().sum()方法統計NaN
方法1:使用isnull().values.any()方法
# importing libraries
import pandas as pd
import numpy as np
num = {'Integers': [10, 15, 30, 40, 55, np.nan,
?? ??? ??? ??? ??? ?75, np.nan, 90, 150, np.nan]}
# Create the dataframe
df = pd.DataFrame(num, columns=['Integers'])
# Applying the method
check_nan = df['Integers'].isnull().values.any()
# printing the result
print(check_nan)
# 輸出 True
可以通過從isnull().values.any()中刪除.values.any()來獲得NaN值所在的確切位置。
df['Integers'].isnull()
1
0 False
1 False
2 False
3 False
4 False
5 True
6 False
7 True
8 False
9 False
10 True
Name: Integers, dtype: bool
方法2:使用isnull().sum()方法
# importing libraries
import pandas as pd
import numpy as np
num = {'Integers': [10, 15, 30, 40, 55, np.nan,
?? ??? ??? ??? ??? ?75, np.nan, 90, 150, np.nan]}
# Create the dataframe
df = pd.DataFrame(num, columns=['Integers'])
# applying the method
count_nan = df['Integers'].isnull().sum()
# printing the number of values present
# in the column
print('Number of NaN values present: ' + str(count_nan))
Number of NaN values present: 3
方法3:使用isnull().sum().any()方法
# importing libraries
import pandas as pd
import numpy as np
nums = {'Integers_1': [10, 15, 30, 40, 55, np.nan, 75,
?? ??? ??? ??? ??? ?np.nan, 90, 150, np.nan],
?? ??? ?'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25,
?? ??? ??? ??? ??? ?np.nan, 26, np.nan, np.nan]}
# Create the dataframe
df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2'])
# applying the method
nan_in_df = df.isnull().sum().any()
# Print the dataframe
print(nan_in_df)
# 輸出 True
可以通過從isnull().sum().any()中刪除.sum().any()來獲得NaN值所在的確切位置。
方法4:使用isnull().sum().sum()方法
# importing libraries
import pandas as pd
import numpy as np
nums = {'Integers_1': [10, 15, 30, 40, 55, np.nan, 75,
?? ??? ??? ??? ??? ?np.nan, 90, 150, np.nan],
?? ??? ?'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25,
?? ??? ??? ??? ??? ?np.nan, 26, np.nan, np.nan]}
# Create the dataframe
df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2'])
# applying the method
nan_in_df = df.isnull().sum().sum()
# printing the number of values present in
# the whole dataframe
print('Number of NaN values present: ' + str(nan_in_df))
Number of NaN values present: 8
參考
https://www.geeksforgeeks.org/check-for-nan-in-pandas-dataframe/
原文鏈接:https://blog.csdn.net/uncle_ll/article/details/128728107
相關推薦
- 2022-11-09 Go語言如何使用golang-jwt/jwt/v4進行JWT鑒權詳解_Golang
- 2022-09-26 Ajax異步請求的五個步驟及實戰案例_AJAX相關
- 2023-07-16 uniapp 小程序 filters 過濾日期
- 2023-01-11 Android?nonTransitiveRClass資源沖突問題淺析_Android
- 2022-05-02 DevOps自動化組件RUNDECK開發部署使用說明_服務器其它
- 2022-01-31 Nginx的平滑重啟和平滑升級
- 2022-10-23 Redis?如何清空所有數據_Redis
- 2022-07-04 python如何使用replace做多字符替換_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同步修改后的遠程分支