Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of non-NaN entries in every column of Dataframe

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 
like image 621
cryp Avatar asked Apr 30 '15 14:04

cryp


People also ask

Which of the following returns the number of non NaN values of series?

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

How do you count non-null values in Python?

Count of non missing value of each column in pandas is created by using notnull(). sum() function as shown below.


2 Answers

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.

like image 68
Alex Riley Avatar answered Oct 19 '22 22:10

Alex Riley


If you want to sum the total count values which are not NAN, one can do;

np.sum(df.count()) 
like image 20
hemanta Avatar answered Oct 19 '22 22:10

hemanta