我想找出我的数据的每一列中NaN的数量。
当前回答
在我的代码中使用@sushmit提出的解决方案。
同样的一种可能的变体也可以是
colNullCnt = []
for z in range(len(df1.cols)):
colNullCnt.append([df1.cols[z], sum(pd.isnull(trainPd[df1.cols[z]]))])
这样做的好处是,它将返回df中每一列的结果。
其他回答
df1.isnull().sum()
这样就可以了。
如果你正在使用Jupyter笔记本,如何....
%%timeit
df.isnull().any().any()
or
%timeit
df.isnull().values.sum()
或者,数据中是否存在nan,如果有,在哪里?
df.isnull().any()
对于第一部分,我们有多种方法计算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)
请使用以下方法计算特定的列数
dataframe.columnName.isnull().sum()
希望这能有所帮助,
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan],'c':[np.nan,2,np.nan], 'd':[np.nan,np.nan,np.nan]})
df.isnull().sum()/len(df) * 100
Thres = 40
(df.isnull().sum()/len(df) * 100 ) < Thres