我想找出我的数据的每一列中NaN的数量。


当前回答

下面是按列计数Null值的代码:

df.isna().sum()

其他回答

你可以从非nan值的计数中减去总长度:

count_nan = len(df) - df.count()

你应该根据你的数据计算时间。与isnull解相比,小级数的速度提高了3倍。

在我的代码中使用@sushmit提出的解决方案。

同样的一种可能的变体也可以是

colNullCnt = []
for z in range(len(df1.cols)):
    colNullCnt.append([df1.cols[z], sum(pd.isnull(trainPd[df1.cols[z]]))])

这样做的好处是,它将返回df中每一列的结果。

对于第一部分,我们有多种方法计算NaN。

方法1计数,由于计数将忽略与大小不同的NaN

print(len(df) - df.count())

方法2:isnull / isna chain with sum

print(df.isnull().sum())
#print(df.isna().sum())

方法3 describe / info:注意这将输出' notull '值计数

print(df.describe())
#print(df.info())

方法。

print(np.count_nonzero(np.isnan(df.values),axis=0))

对于问题的第二部分,如果我们想要在thresh中删除列,我们可以尝试dropna

thresh, optional要求多个非na值。

Thresh = n # no null value require, you can also get the by int(x% * len(df))
df = df.dropna(thresh = Thresh, axis = 1)

下面是按列计数Null值的代码:

df.isna().sum()

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.count.html#pandas.Series.count

pandas.Series.count
Series.count(level=None)[source]

返回系列中非na /null观测值的个数