欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas檢查dataFrame中的NaN實現(xiàn)

 更新時間:2023年01月19日 09:29:07   作者:uncle_ll  
本文主要介紹了Pandas檢查dataFrame中的NaN實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

NaN代表Not A Number,是表示數(shù)據(jù)中缺失值的常用方法之一。它是一種特殊的浮點值,不能轉(zhuǎn)換為浮點數(shù)以外的任何其他類型。

NaN值是數(shù)據(jù)分析中的主要問題之一,為了得到理想的結(jié)果,對NaN進(jìn)行處理是非常必要的。

檢查Pandas DataFrame中的NaN值

在Pandas DataFrame中檢查NaN的方法如下:

  • 使用isnull().values.any()方法檢查NaN
  • 使用isnull().sum()方法統(tǒng)計NaN
  • 使用isnull().sum().any()方法檢查NaN
  • 使用isnull().sum().sum()方法統(tǒng)計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/

到此這篇關(guān)于Pandas檢查dataFrame中的NaN實現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas dataFrame NaN內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python進(jìn)階TensorFlow神經(jīng)網(wǎng)絡(luò)擬合線性及非線性函數(shù)

    python進(jìn)階TensorFlow神經(jīng)網(wǎng)絡(luò)擬合線性及非線性函數(shù)

    這篇文章是python進(jìn)階學(xué)習(xí)主要介紹了TensorFlow神經(jīng)網(wǎng)絡(luò)擬合線性及非線性函數(shù)原理及示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • 一波神奇的Python語句、函數(shù)與方法的使用技巧總結(jié)

    一波神奇的Python語句、函數(shù)與方法的使用技巧總結(jié)

    這篇文章主要介紹了一波神奇的Python函數(shù)與方法的使用技巧總結(jié),包括裝飾器和with語句等的不常見用法,需要的朋友可以參考下
    2015-12-12
  • python中的錯誤如何查看

    python中的錯誤如何查看

    在本篇文章里小編給大家整理的是關(guān)于python中的錯誤如何查看的方法,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • python pygame實現(xiàn)五子棋小游戲

    python pygame實現(xiàn)五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了python pygame實現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 最新評論