我想找出我的数据的每一列中NaN的数量。
当前回答
在我的代码中使用@sushmit提出的解决方案。
同样的一种可能的变体也可以是
colNullCnt = []
for z in range(len(df1.cols)):
colNullCnt.append([df1.cols[z], sum(pd.isnull(trainPd[df1.cols[z]]))])
这样做的好处是,它将返回df中每一列的结果。
其他回答
如果你正在使用Jupyter笔记本,如何....
%%timeit
df.isnull().any().any()
or
%timeit
df.isnull().values.sum()
或者,数据中是否存在nan,如果有,在哪里?
df.isnull().any()
你可以试试:
In [1]: s = pd.DataFrame('a'=[1,2,5, np.nan, np.nan,3],'b'=[1,3, np.nan, np.nan,3,np.nan])
In [4]: s.isna().sum()
Out[4]: out = {'a'=2, 'b'=3} # the number of NaN values for each column
如果需要nan的总和:
In [5]: s.isna().sum().sum()
Out[6]: out = 5 #the inline sum of Out[4]
df1.isnull().sum()
这样就可以了。
df.isnull().sum()
//type: <class 'pandas.core.series.Series'>
or
df.column_name.isnull().sum()
//type: <type 'numpy.int64'>
自从pandas 0.14.1以来,我的建议在value_counts方法中有一个关键字参数已经实现:
import pandas as pd
df = pd.DataFrame({'a':[1,2,np.nan], 'b':[np.nan,1,np.nan]})
for col in df:
print df[col].value_counts(dropna=False)
2 1
1 1
NaN 1
dtype: int64
NaN 2
1 1
dtype: int64
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 确定每列中NA值的个数
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 如何结合多个条件子集数据帧使用“或”?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式