Count Cells of DataFrame

To count non-NA cells in DataFrame in the rows or columns, call count() method on this DataFrame, and specify the axis.

In this tutorial, we will learn the syntax of DataFrame.count() method, and how to use this method to count the number of cells along rows or columns.

The values None, NaN, NaT, and optionally numpy.inf are considered NA. Therefore, these values would not be considered while counting.

Syntax

The syntax of pandas DataFrame.count() method is

</>
Copy
DataFrame.count(axis=0, level=None, numeric_only=False)

where

ParameterValueDescription
axis{0 or ‘index’, 1 or ‘columns’}.
default value is 0.
If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.
levelint or str.
optional.
If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a DataFrame. A str specifies the level name.
numeric_onlybool.
default value is False.
Include only float, int or boolean data.

Return Value

Series or DataFrame.

For each column/row the number of non-NA/null entries. If level is specified returns a DataFrame.

Examples

Count Cells in Columns of DataFrame

In the following program, we take a DataFrame, and count the cells in columns, i.e., axis=0.

Example.py

</>
Copy
import pandas as pd

data = {'col_0': [9, -3, 0, -1, 2], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)

result = df.count()
print(result)

Output

col_0    5
col_1    5
dtype: int64

There are five cells in each column.

Count Cells in Rows of DataFrame

In the following program, we take a DataFrame, and count the cells in rows, i.e., axis=1.

Example.py

</>
Copy
import pandas as pd

data = {'col_0': [9, -3, 0, -1, 2], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)

result = df.count(axis=1)
print(result)

Output

0    2
1    2
2    2
3    2
4    2
dtype: int64

There are two cells in each row.

Count Cells in DataFrame with NA Values

In the following program, we take a DataFrame with some NA values like numpy.nan, and count the cells in columns, i.e., axis=0.

Example.py

</>
Copy
import pandas as pd
import numpy as np

data = {'col_0': [9, -3, 0, np.nan, np.nan], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)

result = df.count()
print(result)

Output

col_0    3
col_1    5
dtype: int64

There are only three non NA values in the first column, and five non NA values in the second column.

Count only Numeric

We can specify to count() method to count only the numeric.

Example.py

</>
Copy
import pandas as pd
import numpy as np

data = {'col_0': [9, -3, 0, 'foo', 'bar'], 'col_1': [-2, 1, 6, 8, -5]}
df = pd.DataFrame(data)

result = df.count(numeric_only=True)
print(result)

Output

col_1    5
dtype: int64

Since col_0 has non-numeric elements, this column is excluded for counting. Only col_1, which has only numeric values is considered for counting.

Conclusion

In this Pandas Tutorial, we learned the syntax of DataFrame.count() method and how to count the cell in DataFrame along columns or rows using pandas DataFrame.count() method.