+2 votes
in Programming Languages by (56.7k points)
I want to find the number of non-NA cells for each column of a dataframe. What function should I use?

1 Answer

+2 votes
by (73.8k points)
selected by
 
Best answer

You can use the count() function of the dataframe. It returns the count of non-NA cells for each column or row depending on the parameter axis.

If the parameter 'axis' is set to 0, counts are generated for each column. If 1, counts are generated for each row.

Here is an example:

>>> import numpy as np

>>> import pandas as pd

>>> df = pd.DataFrame({'A':[12,13,np.nan], 'B':[43,np.nan,34],'C':[np.nan,np.nan,90]})

>>> df

      A     B     C

0  12.0  43.0   NaN

1  13.0   NaN   NaN

2   NaN  34.0  90.0

>>> df.count()

A    2

B    2

C    1

dtype: int64

>>> df.count(axis=0)

A    2

B    2

C    1

dtype: int64

>>> df.count(axis=1)

0    2

1    1

2    2

dtype: int64


...