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

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


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

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

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


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

np.sum(df.count())


In case you are dealing with empty strings you may want to count them as NA as well :

df.replace('', np.nan).count()

or if you also want to remove blank strings :

df.replace(r'^\s*$', np.nan, regex=True).count()