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

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

问题描述

我有一个非常大的DataFrame,我想知道是否有一个或两个内衬的方式来获取一个非NaN条目在DataFrame中的计数。我不想一次做这一列,因为我有近1000列。

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

abd
A 1 2 NaN
B NaN 4 NaN
C 5 NaN 7
D 5 NaN NaN

输出:

  a :3 
b:2
d:1


解决方案

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

 >>> df1.count()
a 3
b 2
d 1​​
dtype: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天全站免登陆