在Dataframe的每个列中计算非nan条目的数量。

时间:2021-02-06 15:50:34

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.

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

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

1 个解决方案

#1


46  

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

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

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

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

同样,count(axis=1)返回每一行中的非nan值的数量。

#1


46  

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

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

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

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

同样,count(axis=1)返回每一行中的非nan值的数量。