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

1 Answer

+1 vote
by (28.5k points)

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


...