计算 Dataframe 每一列中非 NaN 条目的数量 [英] Count number of non-NaN entries in every column of Dataframe

查看:68
本文介绍了计算 Dataframe 每一列中非 NaN 条目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 DataFrame,我想知道是否有短的(一个或两个 liner)方法来获取 DataFrame 中非 NaN 条目的数量.我不想一次做一列,因为我有近 1000 列.

df1 = pd.DataFrame([(1,2,None),(None,4,None),(5,None,7),(5,None,None)],列=['a','b','d'], 索引 = ['A', 'B','C','D'])a b dA 1 2 NaNB NaN 4 NaNC 5 NaN 7D 5 NaN NaN

输出:

a: 3乙:2d:1

解决方案

count() 方法返回每列中非NaN 值的数量:

<预><代码>>>>df1.count()一个 3乙 21数据类型:int64

类似地,count(axis=1) 返回每行中非 NaN 值的数量.

I have a really big DataFrame and I was wondering if there was short (one or two liner) way to get the a count of non-NaN entries in a DataFrame. I don't want to do this one column at a time as I have close to 1000 columns.

df1 = pd.DataFrame([(1,2,None),(None,4,None),(5,None,7),(5,None,None)], 
                    columns=['a','b','d'], index = ['A', 'B','C','D'])

    a   b   d
A   1   2 NaN
B NaN   4 NaN
C   5 NaN   7
D   5 NaN NaN

Output:

a: 3
b: 2
d: 1

解决方案

The count() method returns the number of non-NaN values in each column:

>>> df1.count()
a    3
b    2
d    1
dtype: int64

Similarly, count(axis=1) returns the number of non-NaN values in each row.

这篇关于计算 Dataframe 每一列中非 NaN 条目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆