📌  相关文章
📜  如何计算 Pandas 数据框中列中的 NaN 出现次数?

📅  最后修改于: 2022-05-13 01:55:25.078000             🧑  作者: Mango

如何计算 Pandas 数据框中列中的 NaN 出现次数?

数据帧被分成单元格,这些单元格可以存储属于某个数据结构的值,也可以包含缺失值或 NA 值。 pandas包包含各种内置函数,用于检查数据框单元格中的值是否为 NA,以及对这些 NA 值执行聚合。

方法 #1:在数据帧上使用内置方法isna()sum()

isna()函数用于检测缺失值/无值并返回长度等于应用它的数据框元素的布尔数组,并且sum()方法用于计算这些缺失值的总和。

Python3
# importing necessary packages
import pandas as pd
import numpy as np
  
# creating data
data = [[1, "M", np.nan], [5, "A", 3.2], [
    np.nan, np.nan, 4.6], [1, "D", np.nan]]
  
# converting data to data frame
data_frame = pd.DataFrame(data, 
                          columns=["col1", "col2", "col3"])
  
# printing original data frame
print("\nOriginal Data Frame:")
print(data_frame)
  
# counting NaN values of col1
cnt = data_frame["col1"].isna().sum()
  
# printing count of NaN values
print("\nNan values in col1:", cnt)


Python3
# importing necessary packages
import pandas as pd
import numpy as np
  
# creating data
data = [[1, "M", np.nan], [5, "A", 3.2],
        [np.nan, np.nan, 4.6], [1, "D", np.nan]]
  
# converting data to data frame
data_frame = pd.DataFrame(data, columns=["col1", "col2", "col3"])
  
# printing original data frame
print("\nOriginal Data Frame:")
print(data_frame)
  
# counting NaN values of col1
length = len(data_frame)
count_in_col3 = data_frame['col3'].count()
cnt = length - count_in_col3
  
# printing count of NaN values
print("\nNan in col3:", cnt)


输出:

方法#2:使用数据帧的长度

数据帧的任何特定列中包含的值的计数从数据帧的长度中减去,即数据帧中的行数。 count()方法为我们提供指定列中 NaN 值的总数,length(dataframe) 为我们提供数据帧的长度,即帧中的总行数。

蟒蛇3

# importing necessary packages
import pandas as pd
import numpy as np
  
# creating data
data = [[1, "M", np.nan], [5, "A", 3.2],
        [np.nan, np.nan, 4.6], [1, "D", np.nan]]
  
# converting data to data frame
data_frame = pd.DataFrame(data, columns=["col1", "col2", "col3"])
  
# printing original data frame
print("\nOriginal Data Frame:")
print(data_frame)
  
# counting NaN values of col1
length = len(data_frame)
count_in_col3 = data_frame['col3'].count()
cnt = length - count_in_col3
  
# printing count of NaN values
print("\nNan in col3:", cnt)

输出: